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