blob: e2285c2769b4bab5bd287270d0a1d8e7ed1a4b31 [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"
15
16
17#include "tests/test-common.hpp"
18
19namespace nfd {
20namespace tests {
21
22NFD_LOG_INIT("StrategyChoiceManagerTest");
23
24class DummyStrategy : public fw::Strategy
25{
26public:
27 DummyStrategy(Forwarder& forwarder, const Name& strategyName)
28 : fw::Strategy(forwarder, strategyName)
29 {
30
31 }
32
33 virtual
34 ~DummyStrategy()
35 {
36
37 }
38
39 virtual void
40 afterReceiveInterest(const Face& inFace,
41 const Interest& interest,
42 shared_ptr<fib::Entry> fibEntry,
43 shared_ptr<pit::Entry> pitEntry)
44 {
45
46 }
47};
48
49class TestStrategyA : public DummyStrategy
50{
51public:
52 TestStrategyA(Forwarder& forwarder)
53 : DummyStrategy(forwarder, "/localhost/nfd/strategy/test-strategy-a")
54 {
55 }
56
57 virtual
58 ~TestStrategyA()
59 {
60
61 }
62};
63
64class TestStrategyB : public DummyStrategy
65{
66public:
67 TestStrategyB(Forwarder& forwarder)
68 : DummyStrategy(forwarder, "/localhost/nfd/strategy/test-strategy-b")
69 {
70 }
71
72 virtual
73 ~TestStrategyB()
74 {
75
76 }
77};
78
79class StrategyChoiceManagerFixture : protected BaseFixture
80{
81public:
82
83 StrategyChoiceManagerFixture()
84 : m_nameTree(1024)
85 , m_strategyChoice(m_nameTree, make_shared<TestStrategyA>(boost::ref(m_forwarder)))
86 , m_face(make_shared<InternalFace>())
87 , m_manager(m_strategyChoice, m_face)
88 , m_callbackFired(false)
89 {
90
91 }
92
93 void
94 validateControlResponseCommon(const Data& response,
95 const Name& expectedName,
96 uint32_t expectedCode,
97 const std::string& expectedText,
98 ControlResponse& control)
99 {
100 m_callbackFired = true;
101 Block controlRaw = response.getContent().blockFromValue();
102
103 control.wireDecode(controlRaw);
104
105 NFD_LOG_DEBUG("received control response"
106 << " Name: " << response.getName()
107 << " code: " << control.getCode()
108 << " text: " << control.getText());
109
110 BOOST_CHECK_EQUAL(response.getName(), expectedName);
111 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
112 BOOST_CHECK_EQUAL(control.getText(), expectedText);
113 }
114
115 void
116 validateControlResponse(const Data& response,
117 const Name& expectedName,
118 uint32_t expectedCode,
119 const std::string& expectedText)
120 {
121 ControlResponse control;
122 validateControlResponseCommon(response, expectedName,
123 expectedCode, expectedText, control);
124
125 if (!control.getBody().empty())
126 {
127 BOOST_FAIL("found unexpected control response body");
128 }
129 }
130
131 void
132 validateControlResponse(const Data& response,
133 const Name& expectedName,
134 uint32_t expectedCode,
135 const std::string& expectedText,
136 const Block& expectedBody)
137 {
138 ControlResponse control;
139 validateControlResponseCommon(response, expectedName,
140 expectedCode, expectedText, control);
141
142 BOOST_REQUIRE(!control.getBody().empty());
143 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
144
145 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
146 expectedBody.value_size()) == 0);
147
148 }
149
150 bool
151 didCallbackFire()
152 {
153 return m_callbackFired;
154 }
155
156 void
157 resetCallbackFired()
158 {
159 m_callbackFired = false;
160 }
161
162 shared_ptr<InternalFace>&
163 getFace()
164 {
165 return m_face;
166 }
167
168 StrategyChoiceManager&
169 getManager()
170 {
171 return m_manager;
172 }
173
174 StrategyChoice&
175 getStrategyChoice()
176 {
177 return m_strategyChoice;
178 }
179
180protected:
181 Forwarder m_forwarder;
182 NameTree m_nameTree;
183 StrategyChoice m_strategyChoice;
184 shared_ptr<InternalFace> m_face;
185 StrategyChoiceManager m_manager;
186
187private:
188 bool m_callbackFired;
189};
190
191class AllStrategiesFixture : public StrategyChoiceManagerFixture
192{
193public:
194 AllStrategiesFixture()
195 {
196 m_strategyChoice.install(make_shared<TestStrategyB>(boost::ref(m_forwarder)));
197 }
198
199 virtual
200 ~AllStrategiesFixture()
201 {
202
203 }
204};
205
206BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoiceManager, AllStrategiesFixture)
207
208BOOST_AUTO_TEST_CASE(TestFireInterestFilter)
209{
210 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice"));
211
212 getFace()->onReceiveData +=
213 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
214 command->getName(), 400, "Malformed command");
215
216 getFace()->sendInterest(*command);
217
218 BOOST_REQUIRE(didCallbackFire());
219}
220
221BOOST_AUTO_TEST_CASE(MalformedCommmand)
222{
223 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice"));
224
225 getFace()->onReceiveData +=
226 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
227 command->getName(), 400, "Malformed command");
228
229 getManager().onStrategyChoiceRequest(*command);
230
231 BOOST_REQUIRE(didCallbackFire());
232}
233
234BOOST_AUTO_TEST_CASE(UnsupportedVerb)
235{
236 ndn::nfd::FibManagementOptions options;
237 options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
238
239 Block encodedOptions(options.wireEncode());
240
241 Name commandName("/localhost/nfd/strategy-choice");
242 commandName.append("unsupported");
243 commandName.append(encodedOptions);
244
245 shared_ptr<Interest> command(make_shared<Interest>(commandName));
246
247 getFace()->onReceiveData +=
248 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
249 command->getName(), 501, "Unsupported command");
250
251 getManager().onValidatedStrategyChoiceRequest(command);
252
253 BOOST_REQUIRE(didCallbackFire());
254}
255
256BOOST_AUTO_TEST_CASE(BadOptionParse)
257{
258 Name commandName("/localhost/nfd/strategy-choice");
259 commandName.append("set");
260 commandName.append("NotReallyOptions");
261
262 shared_ptr<Interest> command(make_shared<Interest>(commandName));
263
264 getFace()->onReceiveData +=
265 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
266 command->getName(), 400, "Malformed command");
267
268 getManager().onValidatedStrategyChoiceRequest(command);
269
270 BOOST_REQUIRE(didCallbackFire());
271}
272
273BOOST_AUTO_TEST_CASE(SetStrategies)
274{
275 ndn::nfd::FibManagementOptions options;
276 options.setName("/test");
277 options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
278
279 Block encodedOptions(options.wireEncode());
280
281 Name commandName("/localhost/nfd/strategy-choice");
282 commandName.append("set");
283 commandName.append(encodedOptions);
284
285 shared_ptr<Interest> command(make_shared<Interest>(commandName));
286
287 getFace()->onReceiveData +=
288 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
289 command->getName(), 200, "Success", encodedOptions);
290
291 getManager().onValidatedStrategyChoiceRequest(command);
292
293 BOOST_REQUIRE(didCallbackFire());
294 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
295 BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-b");
296
297 resetCallbackFired();
298 getFace()->onReceiveData.clear();
299}
300
301BOOST_AUTO_TEST_CASE(SetUnsupportedStrategy)
302{
303 ndn::nfd::FibManagementOptions options;
304 options.setName("/test");
305 options.setStrategy("/localhost/nfd/strategy/unit-test-doesnotexist");
306
307 Block encodedOptions(options.wireEncode());
308
309 Name commandName("/localhost/nfd/strategy-choice");
310 commandName.append("set");
311 commandName.append(encodedOptions);
312
313 shared_ptr<Interest> command(make_shared<Interest>(commandName));
314
315 getFace()->onReceiveData +=
316 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
317 command->getName(), 504, "Unsupported strategy");
318
319 getManager().onValidatedStrategyChoiceRequest(command);
320
321 BOOST_REQUIRE(didCallbackFire());
322 fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
323 BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
324}
325
326class DefaultStrategyOnlyFixture : public StrategyChoiceManagerFixture
327{
328public:
329 DefaultStrategyOnlyFixture()
330 : StrategyChoiceManagerFixture()
331 {
332
333 }
334
335 virtual
336 ~DefaultStrategyOnlyFixture()
337 {
338
339 }
340};
341
342
343/// \todo I'm not sure this code branch (code 405) can happen. The manager tests for the strategy first and will return 504.
344// BOOST_FIXTURE_TEST_CASE(SetNotInstalled, DefaultStrategyOnlyFixture)
345// {
346// BOOST_REQUIRE(!getStrategyChoice().hasStrategy("/localhost/nfd/strategy/test-strategy-b"));
347// ndn::nfd::FibManagementOptions options;
348// options.setName("/test");
349// options.setStrategy("/localhost/nfd/strategy/test-strategy-b");
350
351// Block encodedOptions(options.wireEncode());
352
353// Name commandName("/localhost/nfd/strategy-choice");
354// commandName.append("set");
355// commandName.append(encodedOptions);
356
357// shared_ptr<Interest> command(make_shared<Interest>(commandName));
358
359// getFace()->onReceiveData +=
360// bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
361// command->getName(), 405, "Strategy not installed");
362
363// getManager().onValidatedStrategyChoiceRequest(command);
364
365// BOOST_REQUIRE(didCallbackFire());
366// fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test");
367// BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a");
368// }
369
370BOOST_AUTO_TEST_CASE(Unset)
371{
372 ndn::nfd::FibManagementOptions options;
373 options.setName("/test");
374
375 BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b"));
376 BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
377 "/localhost/nfd/strategy/test-strategy-b");
378
379 Block encodedOptions(options.wireEncode());
380
381 Name commandName("/localhost/nfd/strategy-choice");
382 commandName.append("unset");
383 commandName.append(encodedOptions);
384
385 shared_ptr<Interest> command(make_shared<Interest>(commandName));
386
387 getFace()->onReceiveData +=
388 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
389 command->getName(), 200, "Success", encodedOptions);
390
391 getManager().onValidatedStrategyChoiceRequest(command);
392
393 BOOST_REQUIRE(didCallbackFire());
394
395 BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
396 "/localhost/nfd/strategy/test-strategy-a");
397}
398
399BOOST_AUTO_TEST_CASE(UnsetRoot)
400{
401 ndn::nfd::FibManagementOptions options;
402 options.setName("/");
403
404 Block encodedOptions(options.wireEncode());
405
406 Name commandName("/localhost/nfd/strategy-choice");
407 commandName.append("unset");
408 commandName.append(encodedOptions);
409
410 shared_ptr<Interest> command(make_shared<Interest>(commandName));
411
412 getFace()->onReceiveData +=
413 bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1,
414 command->getName(), 403, "Cannot unset root prefix strategy");
415
416 getManager().onValidatedStrategyChoiceRequest(command);
417
418 BOOST_REQUIRE(didCallbackFire());
419
420 BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(),
421 "/localhost/nfd/strategy/test-strategy-a");
422}
423
424BOOST_AUTO_TEST_SUITE_END()
425
426} // namespace tests
427} // namespace nfd