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