blob: 570c1d89de26d8448aea11b3663ec6bb3f89bb7b [file] [log] [blame]
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -07003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070025
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 Afanasyev613e2a92014-04-15 13:36:58 -070033#include "tests/daemon/face/dummy-face.hpp"
34#include "tests/daemon/fw/dummy-strategy.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070035
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060036#include <ndn-cxx/management/nfd-strategy-choice.hpp>
37
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070038#include "tests/test-common.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070039#include "validation-common.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070040
41namespace nfd {
42namespace tests {
43
Steve DiBenedetto77c87512014-10-06 14:18:22 -060044NFD_LOG_INIT("MgmtStrategyChoiceManager");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070045
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070046class StrategyChoiceManagerFixture : protected BaseFixture
47{
48public:
49
50 StrategyChoiceManagerFixture()
Junxiao Shif3c07812014-03-11 21:48:49 -070051 : m_strategyChoice(m_forwarder.getStrategyChoice())
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070052 , m_face(make_shared<InternalFace>())
Vince Lehman5144f822014-07-23 15:12:56 -070053 , m_manager(m_strategyChoice, m_face, m_keyChain)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070054 , m_callbackFired(false)
55 {
Alexander Afanasyevf6980282014-05-13 18:28:40 -070056 m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder),
57 "/localhost/nfd/strategy/test-strategy-a"));
Junxiao Shif3c07812014-03-11 21:48:49 -070058 m_strategyChoice.insert("ndn:/", "/localhost/nfd/strategy/test-strategy-a");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070059 }
60
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070061 virtual
62 ~StrategyChoiceManagerFixture()
63 {
64
65 }
66
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070067 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 DiBenedetto3fff5612014-05-30 15:52:56 -0600124 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 DiBenedetto5330e0d2014-03-05 21:52:51 -0700133 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 DiBenedetto2c2b8892014-02-27 11:46:48 -0700163 void
164 addInterestRule(const std::string& regex,
165 ndn::IdentityCertificate& certificate)
166 {
167 m_manager.addInterestRule(regex, certificate);
168 }
169
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700170protected:
171 Forwarder m_forwarder;
Junxiao Shif3c07812014-03-11 21:48:49 -0700172 StrategyChoice& m_strategyChoice;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700173 shared_ptr<InternalFace> m_face;
174 StrategyChoiceManager m_manager;
Vince Lehman5144f822014-07-23 15:12:56 -0700175 ndn::KeyChain m_keyChain;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700176
177private:
178 bool m_callbackFired;
179};
180
181class AllStrategiesFixture : public StrategyChoiceManagerFixture
182{
183public:
184 AllStrategiesFixture()
185 {
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700186 m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder),
187 "/localhost/nfd/strategy/test-strategy-b"));
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600188
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 DiBenedetto5330e0d2014-03-05 21:52:51 -0700196 }
197
198 virtual
199 ~AllStrategiesFixture()
200 {
201
202 }
203};
204
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700205template <typename T> class AuthorizedCommandFixture : public CommandFixture<T>
206{
207public:
208 AuthorizedCommandFixture()
209 {
210 const std::string regex = "^<localhost><nfd><strategy-choice>";
211 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
212 }
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700213
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700214 virtual
215 ~AuthorizedCommandFixture()
216 {
217
218 }
219};
220
221BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoiceManager,
222 AuthorizedCommandFixture<AllStrategiesFixture>)
223
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700224BOOST_FIXTURE_TEST_CASE(ShortName, AllStrategiesFixture)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700225{
226 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice"));
227
Junxiao Shicd55cde2014-11-13 16:03:24 -0700228 getFace()->onReceiveData += [this, command] (const Data& response) {
229 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
230 };
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700231
232 getFace()->sendInterest(*command);
Junxiao Shi16d1b7d2014-03-27 21:29:09 -0700233 g_io.run_one();
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700234
235 BOOST_REQUIRE(didCallbackFire());
236}
237
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700238BOOST_FIXTURE_TEST_CASE(MalformedCommmand, AllStrategiesFixture)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700239{
240 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice"));
241
Junxiao Shicd55cde2014-11-13 16:03:24 -0700242 getFace()->onReceiveData += [this, command] (const Data& response) {
243 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
244 };
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700245
246 getManager().onStrategyChoiceRequest(*command);
247
248 BOOST_REQUIRE(didCallbackFire());
249}
250
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700251BOOST_FIXTURE_TEST_CASE(UnsignedCommand, AllStrategiesFixture)
252{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600253 ControlParameters parameters;
254 parameters.setName("/test");
255 parameters.setStrategy("/localhost/nfd/strategy/best-route");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700256
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600257 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700258
259 Name commandName("/localhost/nfd/strategy-choice");
260 commandName.append("set");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600261 commandName.append(encodedParameters);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700262
263 shared_ptr<Interest> command(make_shared<Interest>(commandName));
264
Junxiao Shicd55cde2014-11-13 16:03:24 -0700265 getFace()->onReceiveData += [this, command] (const Data& response) {
266 this->validateControlResponse(response, command->getName(), 401, "Signature required");
267 };
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700268
269 getManager().onStrategyChoiceRequest(*command);
270
271 BOOST_REQUIRE(didCallbackFire());
272}
273
274BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand,
275 UnauthorizedCommandFixture<StrategyChoiceManagerFixture>)
276{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600277 ControlParameters parameters;
278 parameters.setName("/test");
279 parameters.setStrategy("/localhost/nfd/strategy/best-route");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700280
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600281 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700282
283 Name commandName("/localhost/nfd/strategy-choice");
284 commandName.append("set");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600285 commandName.append(encodedParameters);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700286
287 shared_ptr<Interest> command(make_shared<Interest>(commandName));
288 generateCommand(*command);
289
Junxiao Shicd55cde2014-11-13 16:03:24 -0700290 getFace()->onReceiveData += [this, command] (const Data& response) {
291 this->validateControlResponse(response, command->getName(), 403, "Unauthorized command");
292 };
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700293
294 getManager().onStrategyChoiceRequest(*command);
295
296 BOOST_REQUIRE(didCallbackFire());
297}
298
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700299BOOST_AUTO_TEST_CASE(UnsupportedVerb)
300{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600301 ControlParameters parameters;
302 parameters.setName("/test");
303 parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700304
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600305 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700306
307 Name commandName("/localhost/nfd/strategy-choice");
308 commandName.append("unsupported");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600309 commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700310
311 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700312 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700313
Junxiao Shicd55cde2014-11-13 16:03:24 -0700314 getFace()->onReceiveData += [this, command] (const Data& response) {
315 this->validateControlResponse(response, command->getName(), 501, "Unsupported command");
316 };
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700317
318 getManager().onValidatedStrategyChoiceRequest(command);
319
320 BOOST_REQUIRE(didCallbackFire());
321}
322
323BOOST_AUTO_TEST_CASE(BadOptionParse)
324{
325 Name commandName("/localhost/nfd/strategy-choice");
326 commandName.append("set");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600327 commandName.append("NotReallyParameters");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700328
329 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700330 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700331
Junxiao Shicd55cde2014-11-13 16:03:24 -0700332 getFace()->onReceiveData += [this, command] (const Data& response) {
333 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
334 };
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700335
336 getManager().onValidatedStrategyChoiceRequest(command);
337
338 BOOST_REQUIRE(didCallbackFire());
339}
340
341BOOST_AUTO_TEST_CASE(SetStrategies)
342{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600343 ControlParameters parameters;
344 parameters.setName("/test");
345 parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700346
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600347 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700348
349 Name commandName("/localhost/nfd/strategy-choice");
350 commandName.append("set");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600351 commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700352
353 shared_ptr<Interest> command(make_shared<Interest>(commandName));
354
Junxiao Shicd55cde2014-11-13 16:03:24 -0700355 getFace()->onReceiveData += [this, command, encodedParameters] (const Data& response) {
356 this->validateControlResponse(response, command->getName(),
357 200, "Success", encodedParameters);
358 };
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700359
360 getManager().onValidatedStrategyChoiceRequest(command);
361
362 BOOST_REQUIRE(didCallbackFire());
363 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
364 BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-b");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600365}
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700366
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600367BOOST_AUTO_TEST_CASE(SetStrategySpecifiedVersion)
368{
369 ControlParameters parameters;
370 parameters.setName("/test");
371 parameters.setStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%01");
372
373 Block encodedParameters(parameters.wireEncode());
374
375 Name commandName("/localhost/nfd/strategy-choice");
376 commandName.append("set");
377 commandName.append(encodedParameters);
378
379 shared_ptr<Interest> command(make_shared<Interest>(commandName));
380
Junxiao Shicd55cde2014-11-13 16:03:24 -0700381 getFace()->onReceiveData += [this, command, encodedParameters] (const Data& response) {
382 this->validateControlResponse(response, command->getName(),
383 200, "Success", encodedParameters);
384 };
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600385
386 getManager().onValidatedStrategyChoiceRequest(command);
387
388 BOOST_REQUIRE(didCallbackFire());
389 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
390 BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-c/%FD%01");
391}
392
393BOOST_AUTO_TEST_CASE(SetStrategyLatestVersion)
394{
395 ControlParameters parameters;
396 parameters.setName("/test");
397 parameters.setStrategy("/localhost/nfd/strategy/test-strategy-c");
398
399 Block encodedParameters(parameters.wireEncode());
400
401 Name commandName("/localhost/nfd/strategy-choice");
402 commandName.append("set");
403 commandName.append(encodedParameters);
404
405 shared_ptr<Interest> command(make_shared<Interest>(commandName));
406
407 ControlParameters responseParameters;
408 responseParameters.setName("/test");
409 responseParameters.setStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%02");
410
Junxiao Shicd55cde2014-11-13 16:03:24 -0700411 getFace()->onReceiveData += [this, command, responseParameters] (const Data& response) {
412 this->validateControlResponse(response, command->getName(),
413 200, "Success", responseParameters.wireEncode());
414 };
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600415
416 getManager().onValidatedStrategyChoiceRequest(command);
417
418 BOOST_REQUIRE(didCallbackFire());
419 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
420 BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-c/%FD%02");
421}
422
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600423BOOST_AUTO_TEST_CASE(SetStrategiesMissingName)
424{
425 ControlParameters parameters;
426 parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b");
427
428 Block encodedParameters(parameters.wireEncode());
429
430 Name commandName("/localhost/nfd/strategy-choice");
431 commandName.append("set");
432 commandName.append(encodedParameters);
433
434 shared_ptr<Interest> command(make_shared<Interest>(commandName));
435
Junxiao Shicd55cde2014-11-13 16:03:24 -0700436 getFace()->onReceiveData += [this, command] (const Data& response) {
437 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
438 };
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600439
440 getManager().onValidatedStrategyChoiceRequest(command);
441
442 BOOST_REQUIRE(didCallbackFire());
443}
444
445BOOST_AUTO_TEST_CASE(SetStrategiesMissingStrategy)
446{
447 ControlParameters parameters;
448 parameters.setName("/test");
449
450 Block encodedParameters(parameters.wireEncode());
451
452 Name commandName("/localhost/nfd/strategy-choice");
453 commandName.append("set");
454 commandName.append(encodedParameters);
455
456 shared_ptr<Interest> command(make_shared<Interest>(commandName));
457
Junxiao Shicd55cde2014-11-13 16:03:24 -0700458 getFace()->onReceiveData += [this, command] (const Data& response) {
459 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
460 };
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600461
462 getManager().onValidatedStrategyChoiceRequest(command);
463
464 BOOST_REQUIRE(didCallbackFire());
465 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
466 BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700467}
468
469BOOST_AUTO_TEST_CASE(SetUnsupportedStrategy)
470{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600471 ControlParameters parameters;
472 parameters.setName("/test");
473 parameters.setStrategy("/localhost/nfd/strategy/unit-test-doesnotexist");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700474
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600475 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700476
477 Name commandName("/localhost/nfd/strategy-choice");
478 commandName.append("set");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600479 commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700480
481 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700482 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700483
Junxiao Shicd55cde2014-11-13 16:03:24 -0700484 getFace()->onReceiveData += [this, command] (const Data& response) {
485 this->validateControlResponse(response, command->getName(), 504, "Unsupported strategy");
486 };
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700487
488 getManager().onValidatedStrategyChoiceRequest(command);
489
490 BOOST_REQUIRE(didCallbackFire());
491 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
492 BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
493}
494
495class DefaultStrategyOnlyFixture : public StrategyChoiceManagerFixture
496{
497public:
498 DefaultStrategyOnlyFixture()
499 : StrategyChoiceManagerFixture()
500 {
501
502 }
503
504 virtual
505 ~DefaultStrategyOnlyFixture()
506 {
507
508 }
509};
510
511
512/// \todo I'm not sure this code branch (code 405) can happen. The manager tests for the strategy first and will return 504.
513// BOOST_FIXTURE_TEST_CASE(SetNotInstalled, DefaultStrategyOnlyFixture)
514// {
515// BOOST_REQUIRE(!getStrategyChoice().hasStrategy("/localhost/nfd/strategy/test-strategy-b"));
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600516// ControlParameters parameters;
517// parameters.setName("/test");
518// parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700519
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600520// Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700521
522// Name commandName("/localhost/nfd/strategy-choice");
523// commandName.append("set");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600524// commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700525
526// shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700527// generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700528
529// getFace()->onReceiveData +=
530// bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
531// command->getName(), 405, "Strategy not installed");
532
533// getManager().onValidatedStrategyChoiceRequest(command);
534
535// BOOST_REQUIRE(didCallbackFire());
536// fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
537// BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
538// }
539
540BOOST_AUTO_TEST_CASE(Unset)
541{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600542 ControlParameters parameters;
543 parameters.setName("/test");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700544
545 BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b"));
546 BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
547 "/localhost/nfd/strategy/test-strategy-b");
548
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600549 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700550
551 Name commandName("/localhost/nfd/strategy-choice");
552 commandName.append("unset");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600553 commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700554
555 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700556 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700557
Junxiao Shicd55cde2014-11-13 16:03:24 -0700558 getFace()->onReceiveData += [this, command, encodedParameters] (const Data& response) {
559 this->validateControlResponse(response, command->getName(),
560 200, "Success", encodedParameters);
561 };
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700562
563 getManager().onValidatedStrategyChoiceRequest(command);
564
565 BOOST_REQUIRE(didCallbackFire());
566
567 BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
568 "/localhost/nfd/strategy/test-strategy-a");
569}
570
571BOOST_AUTO_TEST_CASE(UnsetRoot)
572{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600573 ControlParameters parameters;
574 parameters.setName("/");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700575
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600576 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700577
578 Name commandName("/localhost/nfd/strategy-choice");
579 commandName.append("unset");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600580 commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700581
582 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700583 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700584
Junxiao Shicd55cde2014-11-13 16:03:24 -0700585 getFace()->onReceiveData += [this, command] (const Data& response) {
586 this->validateControlResponse(response, command->getName(),
587 403, "Cannot unset root prefix strategy");
588 };
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700589
590 getManager().onValidatedStrategyChoiceRequest(command);
591
592 BOOST_REQUIRE(didCallbackFire());
593
594 BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
595 "/localhost/nfd/strategy/test-strategy-a");
596}
597
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600598BOOST_AUTO_TEST_CASE(UnsetMissingName)
599{
600 ControlParameters parameters;
601
602 BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b"));
603 BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
604 "/localhost/nfd/strategy/test-strategy-b");
605
606 Block encodedParameters(parameters.wireEncode());
607
608 Name commandName("/localhost/nfd/strategy-choice");
609 commandName.append("unset");
610 commandName.append(encodedParameters);
611
612 shared_ptr<Interest> command(make_shared<Interest>(commandName));
613 generateCommand(*command);
614
Junxiao Shicd55cde2014-11-13 16:03:24 -0700615 getFace()->onReceiveData += [this, command] (const Data& response) {
616 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
617 };
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600618
619 getManager().onValidatedStrategyChoiceRequest(command);
620
621 BOOST_REQUIRE(didCallbackFire());
622
623 BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
624 "/localhost/nfd/strategy/test-strategy-b");
625}
626
Steve DiBenedetto3fff5612014-05-30 15:52:56 -0600627BOOST_AUTO_TEST_CASE(Publish)
628{
629 Name commandName("/localhost/nfd/strategy-choice/list");
630 shared_ptr<Interest> command(make_shared<Interest>(commandName));
631
632 ndn::nfd::StrategyChoice expectedChoice;
633 expectedChoice.setStrategy("/localhost/nfd/strategy/test-strategy-a");
634 expectedChoice.setName("/");
635
636 getFace()->onReceiveData +=
637 bind(&StrategyChoiceManagerFixture::validateList, this, _1, expectedChoice);
638
639 m_manager.onStrategyChoiceRequest(*command);
640 BOOST_REQUIRE(didCallbackFire());
641}
642
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700643BOOST_AUTO_TEST_SUITE_END()
644
645} // namespace tests
646} // namespace nfd