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