blob: 4703fb706daefc1f6ec672ae8be3dd23197bd1b9 [file] [log] [blame]
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "mgmt/strategy-choice-manager.hpp"
8#include "face/face.hpp"
9#include "mgmt/internal-face.hpp"
10#include "table/name-tree.hpp"
11#include "table/strategy-choice.hpp"
12#include "fw/forwarder.hpp"
13#include "fw/strategy.hpp"
14#include "tests/face/dummy-face.hpp"
Junxiao Shif3c07812014-03-11 21:48:49 -070015#include "tests/fw/dummy-strategy.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070016
17#include "tests/test-common.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070018#include "validation-common.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070019
20namespace nfd {
21namespace tests {
22
23NFD_LOG_INIT("StrategyChoiceManagerTest");
24
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070025class StrategyChoiceManagerFixture : protected BaseFixture
26{
27public:
28
29 StrategyChoiceManagerFixture()
Junxiao Shif3c07812014-03-11 21:48:49 -070030 : m_strategyChoice(m_forwarder.getStrategyChoice())
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070031 , m_face(make_shared<InternalFace>())
32 , m_manager(m_strategyChoice, m_face)
33 , m_callbackFired(false)
34 {
Junxiao Shif3c07812014-03-11 21:48:49 -070035 m_strategyChoice.install(make_shared<DummyStrategy>(boost::ref(m_forwarder), "/localhost/nfd/strategy/test-strategy-a"));
36 m_strategyChoice.insert("ndn:/", "/localhost/nfd/strategy/test-strategy-a");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070037 }
38
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070039 virtual
40 ~StrategyChoiceManagerFixture()
41 {
42
43 }
44
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070045 void
46 validateControlResponseCommon(const Data& response,
47 const Name& expectedName,
48 uint32_t expectedCode,
49 const std::string& expectedText,
50 ControlResponse& control)
51 {
52 m_callbackFired = true;
53 Block controlRaw = response.getContent().blockFromValue();
54
55 control.wireDecode(controlRaw);
56
57 NFD_LOG_DEBUG("received control response"
58 << " Name: " << response.getName()
59 << " code: " << control.getCode()
60 << " text: " << control.getText());
61
62 BOOST_CHECK_EQUAL(response.getName(), expectedName);
63 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
64 BOOST_CHECK_EQUAL(control.getText(), expectedText);
65 }
66
67 void
68 validateControlResponse(const Data& response,
69 const Name& expectedName,
70 uint32_t expectedCode,
71 const std::string& expectedText)
72 {
73 ControlResponse control;
74 validateControlResponseCommon(response, expectedName,
75 expectedCode, expectedText, control);
76
77 if (!control.getBody().empty())
78 {
79 BOOST_FAIL("found unexpected control response body");
80 }
81 }
82
83 void
84 validateControlResponse(const Data& response,
85 const Name& expectedName,
86 uint32_t expectedCode,
87 const std::string& expectedText,
88 const Block& expectedBody)
89 {
90 ControlResponse control;
91 validateControlResponseCommon(response, expectedName,
92 expectedCode, expectedText, control);
93
94 BOOST_REQUIRE(!control.getBody().empty());
95 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
96
97 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
98 expectedBody.value_size()) == 0);
99
100 }
101
102 bool
103 didCallbackFire()
104 {
105 return m_callbackFired;
106 }
107
108 void
109 resetCallbackFired()
110 {
111 m_callbackFired = false;
112 }
113
114 shared_ptr<InternalFace>&
115 getFace()
116 {
117 return m_face;
118 }
119
120 StrategyChoiceManager&
121 getManager()
122 {
123 return m_manager;
124 }
125
126 StrategyChoice&
127 getStrategyChoice()
128 {
129 return m_strategyChoice;
130 }
131
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700132 void
133 addInterestRule(const std::string& regex,
134 ndn::IdentityCertificate& certificate)
135 {
136 m_manager.addInterestRule(regex, certificate);
137 }
138
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700139protected:
140 Forwarder m_forwarder;
Junxiao Shif3c07812014-03-11 21:48:49 -0700141 StrategyChoice& m_strategyChoice;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700142 shared_ptr<InternalFace> m_face;
143 StrategyChoiceManager m_manager;
144
145private:
146 bool m_callbackFired;
147};
148
149class AllStrategiesFixture : public StrategyChoiceManagerFixture
150{
151public:
152 AllStrategiesFixture()
153 {
Junxiao Shif3c07812014-03-11 21:48:49 -0700154 m_strategyChoice.install(make_shared<DummyStrategy>(boost::ref(m_forwarder), "/localhost/nfd/strategy/test-strategy-b"));
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700155 }
156
157 virtual
158 ~AllStrategiesFixture()
159 {
160
161 }
162};
163
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700164template <typename T> class AuthorizedCommandFixture : public CommandFixture<T>
165{
166public:
167 AuthorizedCommandFixture()
168 {
169 const std::string regex = "^<localhost><nfd><strategy-choice>";
170 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
171 }
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700172
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700173 virtual
174 ~AuthorizedCommandFixture()
175 {
176
177 }
178};
179
180BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoiceManager,
181 AuthorizedCommandFixture<AllStrategiesFixture>)
182
183BOOST_FIXTURE_TEST_CASE(TestFireInterestFilter, AllStrategiesFixture)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700184{
185 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice"));
186
187 getFace()->onReceiveData +=
188 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
189 command->getName(), 400, "Malformed command");
190
191 getFace()->sendInterest(*command);
192
193 BOOST_REQUIRE(didCallbackFire());
194}
195
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700196BOOST_FIXTURE_TEST_CASE(MalformedCommmand, AllStrategiesFixture)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700197{
198 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice"));
199
200 getFace()->onReceiveData +=
201 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
202 command->getName(), 400, "Malformed command");
203
204 getManager().onStrategyChoiceRequest(*command);
205
206 BOOST_REQUIRE(didCallbackFire());
207}
208
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700209BOOST_FIXTURE_TEST_CASE(UnsignedCommand, AllStrategiesFixture)
210{
Steve DiBenedetto1d75f542014-03-15 19:21:44 -0600211 ndn::nfd::StrategyChoiceOptions options;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700212 options.setName("/test");
213 options.setStrategy("/localhost/nfd/strategy/best-route");
214
215 Block encodedOptions(options.wireEncode());
216
217 Name commandName("/localhost/nfd/strategy-choice");
218 commandName.append("set");
219 commandName.append(encodedOptions);
220
221 shared_ptr<Interest> command(make_shared<Interest>(commandName));
222
223 getFace()->onReceiveData +=
224 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
225 command->getName(), 401, "Signature required");
226
227 getManager().onStrategyChoiceRequest(*command);
228
229 BOOST_REQUIRE(didCallbackFire());
230}
231
232BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand,
233 UnauthorizedCommandFixture<StrategyChoiceManagerFixture>)
234{
Steve DiBenedetto1d75f542014-03-15 19:21:44 -0600235 ndn::nfd::StrategyChoiceOptions options;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700236 options.setName("/test");
237 options.setStrategy("/localhost/nfd/strategy/best-route");
238
239 Block encodedOptions(options.wireEncode());
240
241 Name commandName("/localhost/nfd/strategy-choice");
242 commandName.append("set");
243 commandName.append(encodedOptions);
244
245 shared_ptr<Interest> command(make_shared<Interest>(commandName));
246 generateCommand(*command);
247
248 getFace()->onReceiveData +=
249 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
250 command->getName(), 403, "Unauthorized command");
251
252 getManager().onStrategyChoiceRequest(*command);
253
254 BOOST_REQUIRE(didCallbackFire());
255}
256
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700257BOOST_AUTO_TEST_CASE(UnsupportedVerb)
258{
Steve DiBenedetto1d75f542014-03-15 19:21:44 -0600259 ndn::nfd::StrategyChoiceOptions options;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700260 options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
261
262 Block encodedOptions(options.wireEncode());
263
264 Name commandName("/localhost/nfd/strategy-choice");
265 commandName.append("unsupported");
266 commandName.append(encodedOptions);
267
268 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700269 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700270
271 getFace()->onReceiveData +=
272 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
273 command->getName(), 501, "Unsupported command");
274
275 getManager().onValidatedStrategyChoiceRequest(command);
276
277 BOOST_REQUIRE(didCallbackFire());
278}
279
280BOOST_AUTO_TEST_CASE(BadOptionParse)
281{
282 Name commandName("/localhost/nfd/strategy-choice");
283 commandName.append("set");
284 commandName.append("NotReallyOptions");
285
286 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700287 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700288
289 getFace()->onReceiveData +=
290 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
291 command->getName(), 400, "Malformed command");
292
293 getManager().onValidatedStrategyChoiceRequest(command);
294
295 BOOST_REQUIRE(didCallbackFire());
296}
297
298BOOST_AUTO_TEST_CASE(SetStrategies)
299{
Steve DiBenedetto1d75f542014-03-15 19:21:44 -0600300 ndn::nfd::StrategyChoiceOptions options;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700301 options.setName("/test");
302 options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
303
304 Block encodedOptions(options.wireEncode());
305
306 Name commandName("/localhost/nfd/strategy-choice");
307 commandName.append("set");
308 commandName.append(encodedOptions);
309
310 shared_ptr<Interest> command(make_shared<Interest>(commandName));
311
312 getFace()->onReceiveData +=
313 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
314 command->getName(), 200, "Success", encodedOptions);
315
316 getManager().onValidatedStrategyChoiceRequest(command);
317
318 BOOST_REQUIRE(didCallbackFire());
319 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
320 BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-b");
321
322 resetCallbackFired();
323 getFace()->onReceiveData.clear();
324}
325
326BOOST_AUTO_TEST_CASE(SetUnsupportedStrategy)
327{
Steve DiBenedetto1d75f542014-03-15 19:21:44 -0600328 ndn::nfd::StrategyChoiceOptions options;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700329 options.setName("/test");
330 options.setStrategy("/localhost/nfd/strategy/unit-test-doesnotexist");
331
332 Block encodedOptions(options.wireEncode());
333
334 Name commandName("/localhost/nfd/strategy-choice");
335 commandName.append("set");
336 commandName.append(encodedOptions);
337
338 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700339 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700340
341 getFace()->onReceiveData +=
342 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
343 command->getName(), 504, "Unsupported strategy");
344
345 getManager().onValidatedStrategyChoiceRequest(command);
346
347 BOOST_REQUIRE(didCallbackFire());
348 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
349 BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
350}
351
352class DefaultStrategyOnlyFixture : public StrategyChoiceManagerFixture
353{
354public:
355 DefaultStrategyOnlyFixture()
356 : StrategyChoiceManagerFixture()
357 {
358
359 }
360
361 virtual
362 ~DefaultStrategyOnlyFixture()
363 {
364
365 }
366};
367
368
369/// \todo I'm not sure this code branch (code 405) can happen. The manager tests for the strategy first and will return 504.
370// BOOST_FIXTURE_TEST_CASE(SetNotInstalled, DefaultStrategyOnlyFixture)
371// {
372// BOOST_REQUIRE(!getStrategyChoice().hasStrategy("/localhost/nfd/strategy/test-strategy-b"));
Steve DiBenedetto1d75f542014-03-15 19:21:44 -0600373// ndn::nfd::StrategyChoiceOptions options;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700374// options.setName("/test");
375// options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
376
377// Block encodedOptions(options.wireEncode());
378
379// Name commandName("/localhost/nfd/strategy-choice");
380// commandName.append("set");
381// commandName.append(encodedOptions);
382
383// shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700384// generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700385
386// getFace()->onReceiveData +=
387// bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
388// command->getName(), 405, "Strategy not installed");
389
390// getManager().onValidatedStrategyChoiceRequest(command);
391
392// BOOST_REQUIRE(didCallbackFire());
393// fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
394// BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
395// }
396
397BOOST_AUTO_TEST_CASE(Unset)
398{
Steve DiBenedetto1d75f542014-03-15 19:21:44 -0600399 ndn::nfd::StrategyChoiceOptions options;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700400 options.setName("/test");
401
402 BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b"));
403 BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
404 "/localhost/nfd/strategy/test-strategy-b");
405
406 Block encodedOptions(options.wireEncode());
407
408 Name commandName("/localhost/nfd/strategy-choice");
409 commandName.append("unset");
410 commandName.append(encodedOptions);
411
412 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700413 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700414
415 getFace()->onReceiveData +=
416 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
417 command->getName(), 200, "Success", encodedOptions);
418
419 getManager().onValidatedStrategyChoiceRequest(command);
420
421 BOOST_REQUIRE(didCallbackFire());
422
423 BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
424 "/localhost/nfd/strategy/test-strategy-a");
425}
426
427BOOST_AUTO_TEST_CASE(UnsetRoot)
428{
Steve DiBenedetto1d75f542014-03-15 19:21:44 -0600429 ndn::nfd::StrategyChoiceOptions options;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700430 options.setName("/");
431
432 Block encodedOptions(options.wireEncode());
433
434 Name commandName("/localhost/nfd/strategy-choice");
435 commandName.append("unset");
436 commandName.append(encodedOptions);
437
438 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700439 generateCommand(*command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700440
441 getFace()->onReceiveData +=
442 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
443 command->getName(), 403, "Cannot unset root prefix strategy");
444
445 getManager().onValidatedStrategyChoiceRequest(command);
446
447 BOOST_REQUIRE(didCallbackFire());
448
449 BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
450 "/localhost/nfd/strategy/test-strategy-a");
451}
452
453BOOST_AUTO_TEST_SUITE_END()
454
455} // namespace tests
456} // namespace nfd