Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 24 | |
| 25 | #include "mgmt/strategy-choice-manager.hpp" |
| 26 | #include "face/face.hpp" |
| 27 | #include "mgmt/internal-face.hpp" |
| 28 | #include "table/name-tree.hpp" |
| 29 | #include "table/strategy-choice.hpp" |
| 30 | #include "fw/forwarder.hpp" |
| 31 | #include "fw/strategy.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 32 | #include "tests/daemon/face/dummy-face.hpp" |
| 33 | #include "tests/daemon/fw/dummy-strategy.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 34 | |
| 35 | #include "tests/test-common.hpp" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 36 | #include "validation-common.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 37 | |
| 38 | namespace nfd { |
| 39 | namespace tests { |
| 40 | |
| 41 | NFD_LOG_INIT("StrategyChoiceManagerTest"); |
| 42 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 43 | class StrategyChoiceManagerFixture : protected BaseFixture |
| 44 | { |
| 45 | public: |
| 46 | |
| 47 | StrategyChoiceManagerFixture() |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 48 | : m_strategyChoice(m_forwarder.getStrategyChoice()) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 49 | , m_face(make_shared<InternalFace>()) |
| 50 | , m_manager(m_strategyChoice, m_face) |
| 51 | , m_callbackFired(false) |
| 52 | { |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 53 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 54 | "/localhost/nfd/strategy/test-strategy-a")); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 55 | m_strategyChoice.insert("ndn:/", "/localhost/nfd/strategy/test-strategy-a"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 58 | virtual |
| 59 | ~StrategyChoiceManagerFixture() |
| 60 | { |
| 61 | |
| 62 | } |
| 63 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 64 | void |
| 65 | validateControlResponseCommon(const Data& response, |
| 66 | const Name& expectedName, |
| 67 | uint32_t expectedCode, |
| 68 | const std::string& expectedText, |
| 69 | ControlResponse& control) |
| 70 | { |
| 71 | m_callbackFired = true; |
| 72 | Block controlRaw = response.getContent().blockFromValue(); |
| 73 | |
| 74 | control.wireDecode(controlRaw); |
| 75 | |
| 76 | NFD_LOG_DEBUG("received control response" |
| 77 | << " Name: " << response.getName() |
| 78 | << " code: " << control.getCode() |
| 79 | << " text: " << control.getText()); |
| 80 | |
| 81 | BOOST_CHECK_EQUAL(response.getName(), expectedName); |
| 82 | BOOST_CHECK_EQUAL(control.getCode(), expectedCode); |
| 83 | BOOST_CHECK_EQUAL(control.getText(), expectedText); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | validateControlResponse(const Data& response, |
| 88 | const Name& expectedName, |
| 89 | uint32_t expectedCode, |
| 90 | const std::string& expectedText) |
| 91 | { |
| 92 | ControlResponse control; |
| 93 | validateControlResponseCommon(response, expectedName, |
| 94 | expectedCode, expectedText, control); |
| 95 | |
| 96 | if (!control.getBody().empty()) |
| 97 | { |
| 98 | BOOST_FAIL("found unexpected control response body"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | validateControlResponse(const Data& response, |
| 104 | const Name& expectedName, |
| 105 | uint32_t expectedCode, |
| 106 | const std::string& expectedText, |
| 107 | const Block& expectedBody) |
| 108 | { |
| 109 | ControlResponse control; |
| 110 | validateControlResponseCommon(response, expectedName, |
| 111 | expectedCode, expectedText, control); |
| 112 | |
| 113 | BOOST_REQUIRE(!control.getBody().empty()); |
| 114 | BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size()); |
| 115 | |
| 116 | BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(), |
| 117 | expectedBody.value_size()) == 0); |
| 118 | |
| 119 | } |
| 120 | |
| 121 | bool |
| 122 | didCallbackFire() |
| 123 | { |
| 124 | return m_callbackFired; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | resetCallbackFired() |
| 129 | { |
| 130 | m_callbackFired = false; |
| 131 | } |
| 132 | |
| 133 | shared_ptr<InternalFace>& |
| 134 | getFace() |
| 135 | { |
| 136 | return m_face; |
| 137 | } |
| 138 | |
| 139 | StrategyChoiceManager& |
| 140 | getManager() |
| 141 | { |
| 142 | return m_manager; |
| 143 | } |
| 144 | |
| 145 | StrategyChoice& |
| 146 | getStrategyChoice() |
| 147 | { |
| 148 | return m_strategyChoice; |
| 149 | } |
| 150 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 151 | void |
| 152 | addInterestRule(const std::string& regex, |
| 153 | ndn::IdentityCertificate& certificate) |
| 154 | { |
| 155 | m_manager.addInterestRule(regex, certificate); |
| 156 | } |
| 157 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 158 | protected: |
| 159 | Forwarder m_forwarder; |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 160 | StrategyChoice& m_strategyChoice; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 161 | shared_ptr<InternalFace> m_face; |
| 162 | StrategyChoiceManager m_manager; |
| 163 | |
| 164 | private: |
| 165 | bool m_callbackFired; |
| 166 | }; |
| 167 | |
| 168 | class AllStrategiesFixture : public StrategyChoiceManagerFixture |
| 169 | { |
| 170 | public: |
| 171 | AllStrategiesFixture() |
| 172 | { |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 173 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 174 | "/localhost/nfd/strategy/test-strategy-b")); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | virtual |
| 178 | ~AllStrategiesFixture() |
| 179 | { |
| 180 | |
| 181 | } |
| 182 | }; |
| 183 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 184 | template <typename T> class AuthorizedCommandFixture : public CommandFixture<T> |
| 185 | { |
| 186 | public: |
| 187 | AuthorizedCommandFixture() |
| 188 | { |
| 189 | const std::string regex = "^<localhost><nfd><strategy-choice>"; |
| 190 | T::addInterestRule(regex, *CommandFixture<T>::m_certificate); |
| 191 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 192 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 193 | virtual |
| 194 | ~AuthorizedCommandFixture() |
| 195 | { |
| 196 | |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoiceManager, |
| 201 | AuthorizedCommandFixture<AllStrategiesFixture>) |
| 202 | |
| 203 | BOOST_FIXTURE_TEST_CASE(TestFireInterestFilter, AllStrategiesFixture) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 204 | { |
| 205 | shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice")); |
| 206 | |
| 207 | getFace()->onReceiveData += |
| 208 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 209 | command->getName(), 400, "Malformed command"); |
| 210 | |
| 211 | getFace()->sendInterest(*command); |
Junxiao Shi | 16d1b7d | 2014-03-27 21:29:09 -0700 | [diff] [blame] | 212 | g_io.run_one(); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 213 | |
| 214 | BOOST_REQUIRE(didCallbackFire()); |
| 215 | } |
| 216 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 217 | BOOST_FIXTURE_TEST_CASE(MalformedCommmand, AllStrategiesFixture) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 218 | { |
| 219 | shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice")); |
| 220 | |
| 221 | getFace()->onReceiveData += |
| 222 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 223 | command->getName(), 400, "Malformed command"); |
| 224 | |
| 225 | getManager().onStrategyChoiceRequest(*command); |
| 226 | |
| 227 | BOOST_REQUIRE(didCallbackFire()); |
| 228 | } |
| 229 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 230 | BOOST_FIXTURE_TEST_CASE(UnsignedCommand, AllStrategiesFixture) |
| 231 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 232 | ControlParameters parameters; |
| 233 | parameters.setName("/test"); |
| 234 | parameters.setStrategy("/localhost/nfd/strategy/best-route"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 235 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 236 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 237 | |
| 238 | Name commandName("/localhost/nfd/strategy-choice"); |
| 239 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 240 | commandName.append(encodedParameters); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 241 | |
| 242 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 243 | |
| 244 | getFace()->onReceiveData += |
| 245 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 246 | command->getName(), 401, "Signature required"); |
| 247 | |
| 248 | getManager().onStrategyChoiceRequest(*command); |
| 249 | |
| 250 | BOOST_REQUIRE(didCallbackFire()); |
| 251 | } |
| 252 | |
| 253 | BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, |
| 254 | UnauthorizedCommandFixture<StrategyChoiceManagerFixture>) |
| 255 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 256 | ControlParameters parameters; |
| 257 | parameters.setName("/test"); |
| 258 | parameters.setStrategy("/localhost/nfd/strategy/best-route"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 259 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 260 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 261 | |
| 262 | Name commandName("/localhost/nfd/strategy-choice"); |
| 263 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 264 | commandName.append(encodedParameters); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 265 | |
| 266 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 267 | generateCommand(*command); |
| 268 | |
| 269 | getFace()->onReceiveData += |
| 270 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 271 | command->getName(), 403, "Unauthorized command"); |
| 272 | |
| 273 | getManager().onStrategyChoiceRequest(*command); |
| 274 | |
| 275 | BOOST_REQUIRE(didCallbackFire()); |
| 276 | } |
| 277 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 278 | BOOST_AUTO_TEST_CASE(UnsupportedVerb) |
| 279 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 280 | ControlParameters parameters; |
| 281 | parameters.setName("/test"); |
| 282 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 283 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 284 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 285 | |
| 286 | Name commandName("/localhost/nfd/strategy-choice"); |
| 287 | commandName.append("unsupported"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 288 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 289 | |
| 290 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 291 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 292 | |
| 293 | getFace()->onReceiveData += |
| 294 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 295 | command->getName(), 501, "Unsupported command"); |
| 296 | |
| 297 | getManager().onValidatedStrategyChoiceRequest(command); |
| 298 | |
| 299 | BOOST_REQUIRE(didCallbackFire()); |
| 300 | } |
| 301 | |
| 302 | BOOST_AUTO_TEST_CASE(BadOptionParse) |
| 303 | { |
| 304 | Name commandName("/localhost/nfd/strategy-choice"); |
| 305 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 306 | commandName.append("NotReallyParameters"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 307 | |
| 308 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 309 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 310 | |
| 311 | getFace()->onReceiveData += |
| 312 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 313 | command->getName(), 400, "Malformed command"); |
| 314 | |
| 315 | getManager().onValidatedStrategyChoiceRequest(command); |
| 316 | |
| 317 | BOOST_REQUIRE(didCallbackFire()); |
| 318 | } |
| 319 | |
| 320 | BOOST_AUTO_TEST_CASE(SetStrategies) |
| 321 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 322 | ControlParameters parameters; |
| 323 | parameters.setName("/test"); |
| 324 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 325 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 326 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 327 | |
| 328 | Name commandName("/localhost/nfd/strategy-choice"); |
| 329 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 330 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 331 | |
| 332 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 333 | |
| 334 | getFace()->onReceiveData += |
| 335 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 336 | command->getName(), 200, "Success", encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 337 | |
| 338 | getManager().onValidatedStrategyChoiceRequest(command); |
| 339 | |
| 340 | BOOST_REQUIRE(didCallbackFire()); |
| 341 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 342 | BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 343 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 344 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 345 | BOOST_AUTO_TEST_CASE(SetStrategiesMissingName) |
| 346 | { |
| 347 | ControlParameters parameters; |
| 348 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
| 349 | |
| 350 | Block encodedParameters(parameters.wireEncode()); |
| 351 | |
| 352 | Name commandName("/localhost/nfd/strategy-choice"); |
| 353 | commandName.append("set"); |
| 354 | commandName.append(encodedParameters); |
| 355 | |
| 356 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 357 | |
| 358 | getFace()->onReceiveData += |
| 359 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 360 | command->getName(), 400, "Malformed command"); |
| 361 | |
| 362 | getManager().onValidatedStrategyChoiceRequest(command); |
| 363 | |
| 364 | BOOST_REQUIRE(didCallbackFire()); |
| 365 | } |
| 366 | |
| 367 | BOOST_AUTO_TEST_CASE(SetStrategiesMissingStrategy) |
| 368 | { |
| 369 | ControlParameters parameters; |
| 370 | parameters.setName("/test"); |
| 371 | |
| 372 | Block encodedParameters(parameters.wireEncode()); |
| 373 | |
| 374 | Name commandName("/localhost/nfd/strategy-choice"); |
| 375 | commandName.append("set"); |
| 376 | commandName.append(encodedParameters); |
| 377 | |
| 378 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 379 | |
| 380 | getFace()->onReceiveData += |
| 381 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 382 | command->getName(), 400, "Malformed command"); |
| 383 | |
| 384 | getManager().onValidatedStrategyChoiceRequest(command); |
| 385 | |
| 386 | BOOST_REQUIRE(didCallbackFire()); |
| 387 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 388 | BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | BOOST_AUTO_TEST_CASE(SetUnsupportedStrategy) |
| 392 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 393 | ControlParameters parameters; |
| 394 | parameters.setName("/test"); |
| 395 | parameters.setStrategy("/localhost/nfd/strategy/unit-test-doesnotexist"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 396 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 397 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 398 | |
| 399 | Name commandName("/localhost/nfd/strategy-choice"); |
| 400 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 401 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 402 | |
| 403 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 404 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 405 | |
| 406 | getFace()->onReceiveData += |
| 407 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 408 | command->getName(), 504, "Unsupported strategy"); |
| 409 | |
| 410 | getManager().onValidatedStrategyChoiceRequest(command); |
| 411 | |
| 412 | BOOST_REQUIRE(didCallbackFire()); |
| 413 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 414 | BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 415 | } |
| 416 | |
| 417 | class DefaultStrategyOnlyFixture : public StrategyChoiceManagerFixture |
| 418 | { |
| 419 | public: |
| 420 | DefaultStrategyOnlyFixture() |
| 421 | : StrategyChoiceManagerFixture() |
| 422 | { |
| 423 | |
| 424 | } |
| 425 | |
| 426 | virtual |
| 427 | ~DefaultStrategyOnlyFixture() |
| 428 | { |
| 429 | |
| 430 | } |
| 431 | }; |
| 432 | |
| 433 | |
| 434 | /// \todo I'm not sure this code branch (code 405) can happen. The manager tests for the strategy first and will return 504. |
| 435 | // BOOST_FIXTURE_TEST_CASE(SetNotInstalled, DefaultStrategyOnlyFixture) |
| 436 | // { |
| 437 | // BOOST_REQUIRE(!getStrategyChoice().hasStrategy("/localhost/nfd/strategy/test-strategy-b")); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 438 | // ControlParameters parameters; |
| 439 | // parameters.setName("/test"); |
| 440 | // parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 441 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 442 | // Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 443 | |
| 444 | // Name commandName("/localhost/nfd/strategy-choice"); |
| 445 | // commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 446 | // commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 447 | |
| 448 | // shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 449 | // generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 450 | |
| 451 | // getFace()->onReceiveData += |
| 452 | // bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 453 | // command->getName(), 405, "Strategy not installed"); |
| 454 | |
| 455 | // getManager().onValidatedStrategyChoiceRequest(command); |
| 456 | |
| 457 | // BOOST_REQUIRE(didCallbackFire()); |
| 458 | // fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 459 | // BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 460 | // } |
| 461 | |
| 462 | BOOST_AUTO_TEST_CASE(Unset) |
| 463 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 464 | ControlParameters parameters; |
| 465 | parameters.setName("/test"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 466 | |
| 467 | BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b")); |
| 468 | BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 469 | "/localhost/nfd/strategy/test-strategy-b"); |
| 470 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 471 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 472 | |
| 473 | Name commandName("/localhost/nfd/strategy-choice"); |
| 474 | commandName.append("unset"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 475 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 476 | |
| 477 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 478 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 479 | |
| 480 | getFace()->onReceiveData += |
| 481 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 482 | command->getName(), 200, "Success", encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 483 | |
| 484 | getManager().onValidatedStrategyChoiceRequest(command); |
| 485 | |
| 486 | BOOST_REQUIRE(didCallbackFire()); |
| 487 | |
| 488 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 489 | "/localhost/nfd/strategy/test-strategy-a"); |
| 490 | } |
| 491 | |
| 492 | BOOST_AUTO_TEST_CASE(UnsetRoot) |
| 493 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 494 | ControlParameters parameters; |
| 495 | parameters.setName("/"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 496 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 497 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 498 | |
| 499 | Name commandName("/localhost/nfd/strategy-choice"); |
| 500 | commandName.append("unset"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 501 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 502 | |
| 503 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 504 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 505 | |
| 506 | getFace()->onReceiveData += |
| 507 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 508 | command->getName(), 403, "Cannot unset root prefix strategy"); |
| 509 | |
| 510 | getManager().onValidatedStrategyChoiceRequest(command); |
| 511 | |
| 512 | BOOST_REQUIRE(didCallbackFire()); |
| 513 | |
| 514 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 515 | "/localhost/nfd/strategy/test-strategy-a"); |
| 516 | } |
| 517 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 518 | BOOST_AUTO_TEST_CASE(UnsetMissingName) |
| 519 | { |
| 520 | ControlParameters parameters; |
| 521 | |
| 522 | BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b")); |
| 523 | BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 524 | "/localhost/nfd/strategy/test-strategy-b"); |
| 525 | |
| 526 | Block encodedParameters(parameters.wireEncode()); |
| 527 | |
| 528 | Name commandName("/localhost/nfd/strategy-choice"); |
| 529 | commandName.append("unset"); |
| 530 | commandName.append(encodedParameters); |
| 531 | |
| 532 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 533 | generateCommand(*command); |
| 534 | |
| 535 | getFace()->onReceiveData += |
| 536 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 537 | command->getName(), 400, "Malformed command"); |
| 538 | |
| 539 | getManager().onValidatedStrategyChoiceRequest(command); |
| 540 | |
| 541 | BOOST_REQUIRE(didCallbackFire()); |
| 542 | |
| 543 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 544 | "/localhost/nfd/strategy/test-strategy-b"); |
| 545 | } |
| 546 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 547 | BOOST_AUTO_TEST_SUITE_END() |
| 548 | |
| 549 | } // namespace tests |
| 550 | } // namespace nfd |