blob: b7fdf58d8c3fe3c86dde29076c0e0bdb26533579 [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"
8#include "face/face.hpp"
Alexander Afanasyevbd220a02014-02-20 00:29:56 -08009#include "face/local-face.hpp"
Steve DiBenedetto471c0602014-02-18 12:32:00 -070010#include "mgmt/internal-face.hpp"
11#include "../face/dummy-face.hpp"
12
13#include <algorithm>
14
15#include <boost/test/unit_test.hpp>
16
17namespace nfd {
18
19NFD_LOG_INIT("LocalControlHeaderManagerTest");
20
21class LocalControlHeaderManagerFixture
22{
23public:
24
25 LocalControlHeaderManagerFixture()
26 : m_callbackFired(false)
27 {
28
29 }
30
31 shared_ptr<Face>
32 getFace(FaceId id)
33 {
34 if (id > 0 && id <= m_faces.size())
35 {
36 return m_faces[id-1];
37 }
38 NFD_LOG_DEBUG("No face found returning NULL");
39 return shared_ptr<DummyFace>();
40 }
41
42 void
43 addFace(shared_ptr<Face> face)
44 {
45 m_faces.push_back(face);
46 }
47
48 void
49 validateControlResponse(const Data& response,
50 const Name& expectedName,
51 uint32_t expectedCode,
52 const std::string& expectedText)
53 {
54 m_callbackFired = true;
55
56 ControlResponse control;
57 Block controlRaw = response.getContent().blockFromValue();
58
59 control.wireDecode(controlRaw);
60
61 NFD_LOG_DEBUG("received control response"
62 << " Name: " << response.getName()
63 << " code: " << control.getCode()
64 << " text: " << control.getText());
65
66 BOOST_CHECK_EQUAL(response.getName(), expectedName);
67 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
68 BOOST_CHECK_EQUAL(control.getText(), expectedText);
69
70 if (!control.getBody().empty())
71 {
72 BOOST_FAIL("found unexpected control response body");
73 }
74 }
75
76 bool
77 didCallbackFire()
78 {
79 return m_callbackFired;
80 }
81
82 void
83 resetCallbackFired()
84 {
85 m_callbackFired = false;
86 }
87
88private:
89 std::vector<shared_ptr<Face> > m_faces;
90 bool m_callbackFired;
91};
92
93
94BOOST_AUTO_TEST_SUITE(MgmtLocalControlHeaderManager)
95
96BOOST_FIXTURE_TEST_CASE(InFaceId, LocalControlHeaderManagerFixture)
97{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080098 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -070099 addFace(dummy);
100
101 shared_ptr<InternalFace> face(make_shared<InternalFace>());
102
103 LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
104 face);
105
106 Name enable("/localhost/nfd/control-header/in-faceid/enable");
107
108 face->onReceiveData +=
109 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
110 enable, 200, "Success");
111
112 Interest enableCommand(enable);
113 enableCommand.setIncomingFaceId(1);
114 manager.onLocalControlHeaderRequest(enableCommand);
115
116 BOOST_REQUIRE(didCallbackFire());
117 BOOST_REQUIRE(dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
118 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
119
120 face->onReceiveData.clear();
121 resetCallbackFired();
122
123 Name disable("/localhost/nfd/control-header/in-faceid/disable");
124
125 face->onReceiveData +=
126 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
127 disable, 200, "Success");
128
129 Interest disableCommand(disable);
130 disableCommand.setIncomingFaceId(1);
131 manager.onLocalControlHeaderRequest(disableCommand);
132
133 BOOST_REQUIRE(didCallbackFire());
134 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
135 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
136}
137
138BOOST_FIXTURE_TEST_CASE(NextHopFaceId, LocalControlHeaderManagerFixture)
139{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800140 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700141 addFace(dummy);
142
143 shared_ptr<InternalFace> face(make_shared<InternalFace>());
144
145 LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
146 face);
147
148 Name enable("/localhost/nfd/control-header/nexthop-faceid/enable");
149
150 face->onReceiveData +=
151 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
152 enable, 200, "Success");
153
154 Interest enableCommand(enable);
155 enableCommand.setIncomingFaceId(1);
156 manager.onLocalControlHeaderRequest(enableCommand);
157
158 BOOST_REQUIRE(didCallbackFire());
159 BOOST_REQUIRE(dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
160 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
161
162
163 face->onReceiveData.clear();
164 resetCallbackFired();
165
166 Name disable("/localhost/nfd/control-header/nexthop-faceid/disable");
167
168 face->onReceiveData +=
169 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
170 disable, 200, "Success");
171
172 Interest disableCommand(disable);
173 disableCommand.setIncomingFaceId(1);
174 manager.onLocalControlHeaderRequest(disableCommand);
175
176 BOOST_REQUIRE(didCallbackFire());
177 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
178 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
179}
180
181BOOST_FIXTURE_TEST_CASE(ShortCommand, LocalControlHeaderManagerFixture)
182{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800183 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700184 addFace(dummy);
185
186 shared_ptr<InternalFace> face(make_shared<InternalFace>());
187
188 LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
189 face);
190
191 Name commandName("/localhost/nfd/control-header");
192
193 face->onReceiveData +=
194 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
195 commandName, 400, "Malformed command");
196
197 Interest command(commandName);
198 command.setIncomingFaceId(1);
199 manager.onLocalControlHeaderRequest(command);
200
201 BOOST_REQUIRE(didCallbackFire());
202 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
203 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
204}
205
206BOOST_FIXTURE_TEST_CASE(ShortCommandModule, LocalControlHeaderManagerFixture)
207{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800208 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700209 addFace(dummy);
210
211 shared_ptr<InternalFace> face(make_shared<InternalFace>());
212
213 LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
214 face);
215
216 Name commandName("/localhost/nfd/control-header/in-faceid");
217
218 face->onReceiveData +=
219 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
220 commandName, 400, "Malformed command");
221
222 Interest command(commandName);
223 command.setIncomingFaceId(1);
224 manager.onLocalControlHeaderRequest(command);
225
226 BOOST_REQUIRE(didCallbackFire());
227 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
228 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
229}
230
231BOOST_FIXTURE_TEST_CASE(UnsupportedModule, LocalControlHeaderManagerFixture)
232{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800233 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700234 addFace(dummy);
235
236 shared_ptr<InternalFace> face(make_shared<InternalFace>());
237
238 LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
239 face);
240
241 Name commandName("/localhost/nfd/control-header/madeup/moremadeup");
242
243 face->onReceiveData +=
244 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
245 commandName, 501, "Unsupported");
246
247 Interest command(commandName);
248 command.setIncomingFaceId(1);
249 manager.onLocalControlHeaderRequest(command);
250
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
256BOOST_FIXTURE_TEST_CASE(InFaceIdUnsupportedVerb, LocalControlHeaderManagerFixture)
257{
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
261 shared_ptr<InternalFace> face(make_shared<InternalFace>());
262
263 LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
264 face);
265
266 Name commandName("/localhost/nfd/control-header/in-faceid/madeup");
267
268 face->onReceiveData +=
269 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
270 commandName, 501, "Unsupported");
271
272 Interest command(commandName);
273 command.setIncomingFaceId(1);
274 manager.onLocalControlHeaderRequest(command);
275
276 BOOST_REQUIRE(didCallbackFire());
277 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
278 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
279}
280
281BOOST_FIXTURE_TEST_CASE(NextHopFaceIdUnsupportedVerb, LocalControlHeaderManagerFixture)
282{
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800283 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
Steve DiBenedetto471c0602014-02-18 12:32:00 -0700284 addFace(dummy);
285
286 shared_ptr<InternalFace> face(make_shared<InternalFace>());
287
288 LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
289 face);
290
291 Name commandName("/localhost/nfd/control-header/nexthop-faceid/madeup");
292
293 face->onReceiveData +=
294 bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
295 commandName, 501, "Unsupported");
296
297 Interest command(commandName);
298 command.setIncomingFaceId(1);
299 manager.onLocalControlHeaderRequest(command);
300
301 BOOST_REQUIRE(didCallbackFire());
302 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
303 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
304}
305
306BOOST_AUTO_TEST_SUITE_END()
307
308} // namespace nfd