blob: f8af3bbc525f1a8f3434fff716e4e411c4da00d3 [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
224BOOST_FIXTURE_TEST_CASE(TestFireInterestFilter, AllStrategiesFixture)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700225{
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 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
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 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
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
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
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 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
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
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
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
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
355 getFace()->onReceiveData +=
356 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600357 command->getName(), 200, "Success", encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700358
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 DiBenedetto7564d972014-03-24 14:28:46 -0600364}
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700365
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600366BOOST_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
391BOOST_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 DiBenedetto7564d972014-03-24 14:28:46 -0600420BOOST_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
442BOOST_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 DiBenedetto5330e0d2014-03-05 21:52:51 -0700464}
465
466BOOST_AUTO_TEST_CASE(SetUnsupportedStrategy)
467{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600468 ControlParameters parameters;
469 parameters.setName("/test");
470 parameters.setStrategy("/localhost/nfd/strategy/unit-test-doesnotexist");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700471
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600472 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700473
474 Name commandName("/localhost/nfd/strategy-choice");
475 commandName.append("set");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600476 commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700477
478 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700479 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700480
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
492class DefaultStrategyOnlyFixture : public StrategyChoiceManagerFixture
493{
494public:
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 DiBenedetto7564d972014-03-24 14:28:46 -0600513// ControlParameters parameters;
514// parameters.setName("/test");
515// parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700516
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600517// Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700518
519// Name commandName("/localhost/nfd/strategy-choice");
520// commandName.append("set");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600521// commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700522
523// shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700524// generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700525
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
537BOOST_AUTO_TEST_CASE(Unset)
538{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600539 ControlParameters parameters;
540 parameters.setName("/test");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700541
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 DiBenedetto7564d972014-03-24 14:28:46 -0600546 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700547
548 Name commandName("/localhost/nfd/strategy-choice");
549 commandName.append("unset");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600550 commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700551
552 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700553 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700554
555 getFace()->onReceiveData +=
556 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600557 command->getName(), 200, "Success", encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700558
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
567BOOST_AUTO_TEST_CASE(UnsetRoot)
568{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600569 ControlParameters parameters;
570 parameters.setName("/");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700571
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600572 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700573
574 Name commandName("/localhost/nfd/strategy-choice");
575 commandName.append("unset");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600576 commandName.append(encodedParameters);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700577
578 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700579 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700580
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 DiBenedetto7564d972014-03-24 14:28:46 -0600593BOOST_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 DiBenedetto3fff5612014-05-30 15:52:56 -0600622BOOST_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 DiBenedetto5330e0d2014-03-05 21:52:51 -0700638BOOST_AUTO_TEST_SUITE_END()
639
640} // namespace tests
641} // namespace nfd