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