blob: 168f43f4dd7d2d63b2797844343ff77d51f20835 [file] [log] [blame]
Steve DiBenedetto471c0602014-02-18 12:32:00 -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/local-control-header-manager.hpp"
Steve DiBenedetto471c0602014-02-18 12:32:00 -07008#include "mgmt/internal-face.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -07009#include "tests/face/dummy-face.hpp"
Steve DiBenedetto471c0602014-02-18 12:32:00 -070010
Junxiao Shid9ee45c2014-02-27 15:38:11 -070011#include "tests/test-common.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070012#include "validation-common.hpp"
Steve DiBenedetto471c0602014-02-18 12:32:00 -070013
14namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070015namespace tests {
Steve DiBenedetto471c0602014-02-18 12:32:00 -070016
17NFD_LOG_INIT("LocalControlHeaderManagerTest");
18
Junxiao Shid9ee45c2014-02-27 15:38:11 -070019class LocalControlHeaderManagerFixture : protected BaseFixture
Steve DiBenedetto471c0602014-02-18 12:32:00 -070020{
21public:
22
Steve DiBenedetto471c0602014-02-18 12:32:00 -070023 shared_ptr<Face>
24 getFace(FaceId id)
25 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070026 if (id > 0 && static_cast<size_t>(id) <= m_faces.size())
Steve DiBenedetto471c0602014-02-18 12:32:00 -070027 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028 return m_faces[id - 1];
Steve DiBenedetto471c0602014-02-18 12:32:00 -070029 }
30 NFD_LOG_DEBUG("No face found returning NULL");
31 return shared_ptr<DummyFace>();
32 }
33
34 void
35 addFace(shared_ptr<Face> face)
36 {
37 m_faces.push_back(face);
38 }
39
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070040 shared_ptr<InternalFace>
41 getInternalFace()
42 {
43 return m_face;
44 }
45
46 LocalControlHeaderManager&
47 getManager()
48 {
49 return m_manager;
50 }
51
52 void
53 addInterestRule(const std::string& regex,
54 ndn::IdentityCertificate& certificate)
55 {
56 m_manager.addInterestRule(regex, certificate);
57 }
58
Steve DiBenedetto471c0602014-02-18 12:32:00 -070059 void
60 validateControlResponse(const Data& response,
61 const Name& expectedName,
62 uint32_t expectedCode,
63 const std::string& expectedText)
64 {
65 m_callbackFired = true;
66
67 ControlResponse control;
68 Block controlRaw = response.getContent().blockFromValue();
69
70 control.wireDecode(controlRaw);
71
72 NFD_LOG_DEBUG("received control response"
73 << " Name: " << response.getName()
74 << " code: " << control.getCode()
75 << " text: " << control.getText());
76
77 BOOST_CHECK_EQUAL(response.getName(), expectedName);
78 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
79 BOOST_CHECK_EQUAL(control.getText(), expectedText);
80
81 if (!control.getBody().empty())
82 {
83 BOOST_FAIL("found unexpected control response body");
84 }
85 }
86
87 bool
88 didCallbackFire()
89 {
90 return m_callbackFired;
91 }
92
93 void
94 resetCallbackFired()
95 {
96 m_callbackFired = false;
97 }
98
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070099protected:
100 LocalControlHeaderManagerFixture()
101 : m_face(make_shared<InternalFace>()),
102 m_manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
103 m_face),
104 m_callbackFired(false)
105 {
106 }
107
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700108private:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700109 shared_ptr<InternalFace> m_face;
110 LocalControlHeaderManager m_manager;
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700111 std::vector<shared_ptr<Face> > m_faces;
112 bool m_callbackFired;
113};
114
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700115template <typename T> class AuthorizedCommandFixture:
116 public CommandFixture<T>
117{
118public:
119 AuthorizedCommandFixture()
120 {
121 const std::string regex = "^<localhost><nfd><control-header>";
122 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
123 }
124
125 virtual
126 ~AuthorizedCommandFixture()
127 {
128 }
129};
130
131BOOST_FIXTURE_TEST_SUITE(MgmtLocalControlHeaderManager,
132 AuthorizedCommandFixture<LocalControlHeaderManagerFixture>)
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700133
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700134BOOST_AUTO_TEST_CASE(InFaceId)
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700135{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800136 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700137 addFace(dummy);
138
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700139 Name enable("/localhost/nfd/control-header/in-faceid/enable");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700140 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
141 enableCommand->setIncomingFaceId(1);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700142
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700143 generateCommand(*enableCommand);
144
145 getInternalFace()->onReceiveData +=
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700146 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700147 enableCommand->getName(), 200, "Success");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700148
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700149 getManager().onLocalControlHeaderRequest(*enableCommand);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700150
151 BOOST_REQUIRE(didCallbackFire());
152 BOOST_REQUIRE(dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
153 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
154
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700155 getInternalFace()->onReceiveData.clear();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700156 resetCallbackFired();
157
158 Name disable("/localhost/nfd/control-header/in-faceid/disable");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700159 shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
160 disableCommand->setIncomingFaceId(1);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700161
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700162 generateCommand(*disableCommand);
163
164 getInternalFace()->onReceiveData +=
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700165 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700166 disableCommand->getName(), 200, "Success");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700167
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700168 getManager().onLocalControlHeaderRequest(*disableCommand);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700169
170 BOOST_REQUIRE(didCallbackFire());
171 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
172 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
173}
174
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700175BOOST_AUTO_TEST_CASE(NextHopFaceId)
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700176{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800177 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700178 addFace(dummy);
179
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700180 Name enable("/localhost/nfd/control-header/nexthop-faceid/enable");
181
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700182 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
183 enableCommand->setIncomingFaceId(1);
184 generateCommand(*enableCommand);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700185
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700186 getInternalFace()->onReceiveData +=
187 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
188 enableCommand->getName(), 200, "Success");
189
190 getManager().onLocalControlHeaderRequest(*enableCommand);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700191
192 BOOST_REQUIRE(didCallbackFire());
193 BOOST_REQUIRE(dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
194 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
195
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700196 getInternalFace()->onReceiveData.clear();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700197 resetCallbackFired();
198
199 Name disable("/localhost/nfd/control-header/nexthop-faceid/disable");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700200 shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
201 disableCommand->setIncomingFaceId(1);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700202
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700203 generateCommand(*disableCommand);
204
205 getInternalFace()->onReceiveData +=
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700206 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700207 disableCommand->getName(), 200, "Success");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700208
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700209 getManager().onLocalControlHeaderRequest(*disableCommand);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700210
211 BOOST_REQUIRE(didCallbackFire());
212 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
213 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
214}
215
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700216BOOST_AUTO_TEST_CASE(ShortCommand)
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700217{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800218 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700219 addFace(dummy);
220
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700221 Name commandName("/localhost/nfd/control-header");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700222 Interest command(commandName);
223 command.setIncomingFaceId(1);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700224
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700225 getInternalFace()->onReceiveData +=
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700226 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
227 commandName, 400, "Malformed command");
228
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700229 getManager().onLocalControlHeaderRequest(command);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700230
231 BOOST_REQUIRE(didCallbackFire());
232 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
233 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
234}
235
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700236BOOST_AUTO_TEST_CASE(ShortCommandModule)
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700237{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800238 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700239 addFace(dummy);
240
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700241 Name commandName("/localhost/nfd/control-header/in-faceid");
242
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700243 getInternalFace()->onReceiveData +=
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700244 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
245 commandName, 400, "Malformed command");
246
247 Interest command(commandName);
248 command.setIncomingFaceId(1);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700249 getManager().onLocalControlHeaderRequest(command);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700250
251 BOOST_REQUIRE(didCallbackFire());
252 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
253 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
254}
255
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700256BOOST_AUTO_TEST_CASE(UnsupportedModule)
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700257{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800258 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700259 addFace(dummy);
260
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700261 Name commandName("/localhost/nfd/control-header/madeup/moremadeup");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700262 shared_ptr<Interest> command(make_shared<Interest>(commandName));
263 command->setIncomingFaceId(1);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700264
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700265 generateCommand(*command);
266
267 getInternalFace()->onReceiveData +=
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700268 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700269 command->getName(), 501, "Unsupported");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700270
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700271 getManager().onLocalControlHeaderRequest(*command);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700272
273 BOOST_REQUIRE(didCallbackFire());
274 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
275 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
276}
277
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700278BOOST_AUTO_TEST_CASE(InFaceIdUnsupportedVerb)
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700279{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800280 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700281 addFace(dummy);
282
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700283 Name commandName("/localhost/nfd/control-header/in-faceid/madeup");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700284 shared_ptr<Interest> command(make_shared<Interest>(commandName));
285 command->setIncomingFaceId(1);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700286
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700287
288 generateCommand(*command);
289
290 getInternalFace()->onReceiveData +=
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700291 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700292 command->getName(), 501, "Unsupported");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700293
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700294 getManager().onLocalControlHeaderRequest(*command);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700295
296 BOOST_REQUIRE(didCallbackFire());
297 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
298 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
299}
300
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700301BOOST_AUTO_TEST_CASE(NextHopFaceIdUnsupportedVerb)
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700302{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800303 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700304 addFace(dummy);
305
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700306 Name commandName("/localhost/nfd/control-header/nexthop-faceid/madeup");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700307 shared_ptr<Interest> command(make_shared<Interest>(commandName));
308 command->setIncomingFaceId(1);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700309
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700310 generateCommand(*command);
311
312 getInternalFace()->onReceiveData +=
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700313 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700314 command->getName(), 501, "Unsupported");
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700315
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700316 getManager().onLocalControlHeaderRequest(*command);
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700317
318 BOOST_REQUIRE(didCallbackFire());
319 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
320 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
321}
322
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700323BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand,
324 UnauthorizedCommandFixture<LocalControlHeaderManagerFixture>)
325{
326 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
327 addFace(dummy);
328
329 Name enable("/localhost/nfd/control-header/in-faceid/enable");
330 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
331 enableCommand->setIncomingFaceId(1);
332
333 generateCommand(*enableCommand);
334
335 getInternalFace()->onReceiveData +=
336 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
337 enableCommand->getName(), 403, "Unauthorized command");
338
339 getManager().onLocalControlHeaderRequest(*enableCommand);
340
341 BOOST_REQUIRE(didCallbackFire());
342 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
343}
344
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700345BOOST_AUTO_TEST_SUITE_END()
346
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700347} // namespace tests
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700348} // namespace nfd