blob: aadb2c44e14a9d2849c2eab921cabb28fa685d3d [file] [log] [blame]
Junxiao Shi034c1882016-06-24 18:06:51 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "mgmt/nfd/status-dataset.hpp"
23#include "mgmt/nfd/controller.hpp"
Junxiao Shi034c1882016-06-24 18:06:51 +000024
Junxiao Shi7357ef22016-09-07 02:39:37 +000025#include "controller-fixture.hpp"
26#include "../../make-interest-data.hpp"
Junxiao Shi034c1882016-06-24 18:06:51 +000027
28namespace ndn {
29namespace nfd {
30namespace tests {
31
Junxiao Shi85d90832016-08-04 03:19:46 +000032using namespace ndn::tests;
Junxiao Shi034c1882016-06-24 18:06:51 +000033
Junxiao Shi7357ef22016-09-07 02:39:37 +000034BOOST_AUTO_TEST_SUITE(Mgmt)
35BOOST_AUTO_TEST_SUITE(Nfd)
Junxiao Shi034c1882016-06-24 18:06:51 +000036
37class ControllerStatusDatasetFixture : public ControllerFixture
38{
39protected:
40 /** \brief send one WireEncodable as Data reply
41 * \param prefix dataset prefix without version and segment
42 * \param payload payload block
43 * \note payload must fit in one Data
44 */
45 template<typename T>
46 void
47 sendDataset(const Name& prefix, const T& payload)
48 {
49 BOOST_CONCEPT_ASSERT((WireEncodable<T>));
50
51 auto data = this->prepareDatasetReply(prefix);
52 data->setContent(payload.wireEncode());
53 face.receive(*signData(data));
54 }
55
56 /** \brief send two WireEncodables as Data reply
57 * \param prefix dataset prefix without version and segment
58 * \param payload1 first vector item
59 * \param payload2 second vector item
60 * \note all payloads must fit in one Data
61 */
62 template<typename T1, typename T2>
63 void
64 sendDataset(const Name& prefix, const T1& payload1, const T2& payload2)
65 {
66 // The test suite allows up to two items, and put them in the same Data packet,
67 // because this test suite focuses on Controller::fetch<StatusDataset>,
68 // and is not intended to cover SegmentFetcher behavior.
69
70 BOOST_CONCEPT_ASSERT((WireEncodable<T1>));
71 BOOST_CONCEPT_ASSERT((WireEncodable<T2>));
72
73 ndn::encoding::EncodingBuffer buffer;
74 payload2.wireEncode(buffer);
75 payload1.wireEncode(buffer);
76
77 auto data = this->prepareDatasetReply(prefix);
78 data->setContent(buffer.buf(), buffer.size());
79 face.receive(*signData(data));
80 }
81
82private:
83 shared_ptr<Data>
84 prepareDatasetReply(const Name& prefix)
85 {
86 Name name = prefix;
87 name.appendVersion().appendSegment(0);
88
89 // These warnings assist in debugging a `hasResult` check failure.
90 // They usually indicate a misspelled prefix or incorrect timing in the test case.
91 if (face.sentInterests.size() < 1) {
92 BOOST_WARN_MESSAGE(false, "no Interest expressed");
93 }
94 else {
95 BOOST_WARN_MESSAGE(face.sentInterests.back().getName().isPrefixOf(name),
96 "last Interest " << face.sentInterests.back().getName() <<
97 " cannot be satisfied by this Data " << name);
98 }
99
100 auto data = make_shared<Data>(name);
101 data->setFinalBlockId(data->getName()[-1]);
102 return data;
103 }
104};
105
Junxiao Shi7357ef22016-09-07 02:39:37 +0000106BOOST_FIXTURE_TEST_SUITE(TestStatusDataset, ControllerStatusDatasetFixture)
Junxiao Shi034c1882016-06-24 18:06:51 +0000107
108BOOST_AUTO_TEST_SUITE(Failures)
109
110BOOST_AUTO_TEST_CASE(Timeout)
111{
112 CommandOptions options;
113 options.setTimeout(time::milliseconds(3000));
114 controller.fetch<FaceDataset>(
115 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000116 datasetFailCallback,
Junxiao Shi034c1882016-06-24 18:06:51 +0000117 options);
118 this->advanceClocks(time::milliseconds(500), 7);
119
120 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
121 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_TIMEOUT);
122}
123
124BOOST_AUTO_TEST_CASE(DataHasNoSegment)
125{
126 controller.fetch<FaceDataset>(
127 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000128 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000129 this->advanceClocks(time::milliseconds(500));
130
131 face.receive(*makeData("/localhost/nfd/faces/list/%FD%00"));
132 this->advanceClocks(time::milliseconds(500));
133
134 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
135 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
136}
137
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000138BOOST_AUTO_TEST_CASE(ValidationFailure)
139{
140 this->setValidationResult(false);
141
142 controller.fetch<FaceDataset>(
143 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000144 datasetFailCallback);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000145 this->advanceClocks(time::milliseconds(500));
146
147 FaceStatus payload;
148 payload.setFaceId(5744);
149 this->sendDataset("/localhost/nfd/faces/list", payload);
150 this->advanceClocks(time::milliseconds(500));
151
152 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
153 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_VALIDATION);
154}
155
Junxiao Shi034c1882016-06-24 18:06:51 +0000156BOOST_AUTO_TEST_CASE(Nack)
157{
158 controller.fetch<FaceDataset>(
159 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000160 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000161 this->advanceClocks(time::milliseconds(500));
162
163 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
164 face.receive(lp::Nack(face.sentInterests.back()));
165 this->advanceClocks(time::milliseconds(500));
166
167 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
168 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_NACK);
169}
170
171BOOST_AUTO_TEST_CASE(ParseError1)
172{
173 controller.fetch<FaceDataset>(
174 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000175 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000176 this->advanceClocks(time::milliseconds(500));
177
178 Name payload; // Name is not valid FaceStatus
179 this->sendDataset("/localhost/nfd/faces/list", payload);
180 this->advanceClocks(time::milliseconds(500));
181
182 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
183 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
184}
185
186BOOST_AUTO_TEST_CASE(ParseError2)
187{
188 controller.fetch<FaceDataset>(
189 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000190 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000191 this->advanceClocks(time::milliseconds(500));
192
193 FaceStatus payload1;
194 payload1.setFaceId(10930);
195 Name payload2; // Name is not valid FaceStatus
196 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
197 this->advanceClocks(time::milliseconds(500));
198
199 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
200 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
201}
202
203BOOST_AUTO_TEST_SUITE_END() // Failures
204
Junxiao Shi600f7112016-07-16 11:57:18 +0000205BOOST_AUTO_TEST_SUITE(NoCallback)
206
207BOOST_AUTO_TEST_CASE(Success)
208{
209 controller.fetch<FaceDataset>(
210 nullptr,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000211 datasetFailCallback);
Junxiao Shi600f7112016-07-16 11:57:18 +0000212 this->advanceClocks(time::milliseconds(500));
213
214 FaceStatus payload;
215 payload.setFaceId(2577);
216 this->sendDataset("/localhost/nfd/faces/list", payload);
217 BOOST_CHECK_NO_THROW(this->advanceClocks(time::milliseconds(500)));
218
219 BOOST_CHECK_EQUAL(failCodes.size(), 0);
220}
221
222BOOST_AUTO_TEST_CASE(Failure)
223{
224 CommandOptions options;
225 options.setTimeout(time::milliseconds(3000));
226 controller.fetch<FaceDataset>(
227 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
228 nullptr,
229 options);
230 BOOST_CHECK_NO_THROW(this->advanceClocks(time::milliseconds(500), 7));
231}
232
233BOOST_AUTO_TEST_SUITE_END() // NoCallback
234
Junxiao Shi034c1882016-06-24 18:06:51 +0000235BOOST_AUTO_TEST_SUITE(Datasets)
236
237BOOST_AUTO_TEST_CASE(StatusGeneral)
238{
239 bool hasResult = false;
240 controller.fetch<ForwarderGeneralStatusDataset>(
241 [&hasResult] (const ForwarderStatus& result) {
242 hasResult = true;
243 BOOST_CHECK_EQUAL(result.getNfdVersion(), "0.4.2");
244 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000245 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000246 this->advanceClocks(time::milliseconds(500));
247
248 ForwarderStatus payload;
249 payload.setNfdVersion("0.4.2");
250 this->sendDataset("/localhost/nfd/status/general", payload);
251 this->advanceClocks(time::milliseconds(500));
252
253 BOOST_CHECK(hasResult);
254 BOOST_CHECK_EQUAL(failCodes.size(), 0);
255}
256
257BOOST_AUTO_TEST_CASE(FaceList)
258{
259 bool hasResult = false;
260 controller.fetch<FaceDataset>(
261 [&hasResult] (const std::vector<FaceStatus>& result) {
262 hasResult = true;
263 BOOST_CHECK_EQUAL(result.size(), 2);
264 BOOST_CHECK_EQUAL(result.front().getFaceId(), 24485);
265 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000266 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000267 this->advanceClocks(time::milliseconds(500));
268
269 FaceStatus payload1;
270 payload1.setFaceId(24485);
271 FaceStatus payload2;
272 payload2.setFaceId(12987);
273 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
274 this->advanceClocks(time::milliseconds(500));
275
276 BOOST_CHECK(hasResult);
277 BOOST_CHECK_EQUAL(failCodes.size(), 0);
278}
279
280BOOST_AUTO_TEST_CASE(FaceQuery)
281{
282 FaceQueryFilter filter;
283 filter.setUriScheme("udp4");
284 bool hasResult = false;
285 controller.fetch<FaceQueryDataset>(
286 filter,
287 [&hasResult] (const std::vector<FaceStatus>& result) {
288 hasResult = true;
289 BOOST_CHECK_EQUAL(result.size(), 1);
290 BOOST_CHECK_EQUAL(result.front().getFaceId(), 8795);
291 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000292 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000293 this->advanceClocks(time::milliseconds(500));
294
295 Name prefix("/localhost/nfd/faces/query");
296 prefix.append(filter.wireEncode());
297 FaceStatus payload;
298 payload.setFaceId(8795);
299 this->sendDataset(prefix, payload);
300 this->advanceClocks(time::milliseconds(500));
301
302 BOOST_CHECK(hasResult);
303 BOOST_CHECK_EQUAL(failCodes.size(), 0);
304}
305
306BOOST_AUTO_TEST_CASE(FaceQueryWithOptions)
307{
308 FaceQueryFilter filter;
309 filter.setUriScheme("udp4");
310 CommandOptions options;
311 options.setTimeout(time::milliseconds(3000));
312 bool hasResult = false;
313 controller.fetch<FaceQueryDataset>(
314 filter,
315 [&hasResult] (const std::vector<FaceStatus>& result) {
316 hasResult = true;
317 BOOST_CHECK_EQUAL(result.size(), 1);
318 BOOST_CHECK_EQUAL(result.front().getFaceId(), 14022);
319 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000320 datasetFailCallback,
Junxiao Shi034c1882016-06-24 18:06:51 +0000321 options);
322 this->advanceClocks(time::milliseconds(500));
323
324 Name prefix("/localhost/nfd/faces/query");
325 prefix.append(filter.wireEncode());
326 FaceStatus payload;
327 payload.setFaceId(14022);
328 this->sendDataset(prefix, payload);
329 this->advanceClocks(time::milliseconds(500));
330
331 BOOST_CHECK(hasResult);
332 BOOST_CHECK_EQUAL(failCodes.size(), 0);
333}
334
Junxiao Shi1eacb972016-07-07 22:51:07 +0000335BOOST_AUTO_TEST_CASE(FaceChannels)
336{
337 bool hasResult = false;
338 controller.fetch<ChannelDataset>(
339 [&hasResult] (const std::vector<ChannelStatus>& result) {
340 hasResult = true;
341 BOOST_CHECK_EQUAL(result.size(), 2);
342 BOOST_CHECK_EQUAL(result.front().getLocalUri(), "tcp4://192.0.2.1:6363");
343 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000344 datasetFailCallback);
Junxiao Shi1eacb972016-07-07 22:51:07 +0000345 this->advanceClocks(time::milliseconds(500));
346
347 ChannelStatus payload1;
348 payload1.setLocalUri("tcp4://192.0.2.1:6363");
349 ChannelStatus payload2;
350 payload2.setLocalUri("udp4://192.0.2.1:6363");
351 this->sendDataset("/localhost/nfd/faces/channels", payload1, payload2);
352 this->advanceClocks(time::milliseconds(500));
353
354 BOOST_CHECK(hasResult);
355 BOOST_CHECK_EQUAL(failCodes.size(), 0);
356}
357
Junxiao Shi034c1882016-06-24 18:06:51 +0000358BOOST_AUTO_TEST_CASE(FibList)
359{
360 bool hasResult = false;
361 controller.fetch<FibDataset>(
362 [&hasResult] (const std::vector<FibEntry>& result) {
363 hasResult = true;
364 BOOST_CHECK_EQUAL(result.size(), 2);
365 BOOST_CHECK_EQUAL(result.front().getPrefix(), "/wYs7fzYcfG");
366 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000367 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000368 this->advanceClocks(time::milliseconds(500));
369
370 FibEntry payload1;
371 payload1.setPrefix("/wYs7fzYcfG");
372 FibEntry payload2;
373 payload2.setPrefix("/LKvmnzY5S");
374 this->sendDataset("/localhost/nfd/fib/list", payload1, payload2);
375 this->advanceClocks(time::milliseconds(500));
376
377 BOOST_CHECK(hasResult);
378 BOOST_CHECK_EQUAL(failCodes.size(), 0);
379}
380
381BOOST_AUTO_TEST_CASE(StrategyChoiceList)
382{
383 bool hasResult = false;
384 controller.fetch<StrategyChoiceDataset>(
385 [&hasResult] (const std::vector<StrategyChoice>& result) {
386 hasResult = true;
387 BOOST_CHECK_EQUAL(result.size(), 2);
388 BOOST_CHECK_EQUAL(result.front().getName(), "/8MLz6N3B");
389 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000390 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000391 this->advanceClocks(time::milliseconds(500));
392
393 StrategyChoice payload1;
394 payload1.setName("/8MLz6N3B");
395 StrategyChoice payload2;
396 payload2.setName("/svqcBu0YwU");
397 this->sendDataset("/localhost/nfd/strategy-choice/list", payload1, payload2);
398 this->advanceClocks(time::milliseconds(500));
399
400 BOOST_CHECK(hasResult);
401 BOOST_CHECK_EQUAL(failCodes.size(), 0);
402}
403
404BOOST_AUTO_TEST_CASE(RibList)
405{
406 bool hasResult = false;
407 controller.fetch<RibDataset>(
408 [&hasResult] (const std::vector<RibEntry>& result) {
409 hasResult = true;
410 BOOST_CHECK_EQUAL(result.size(), 2);
411 BOOST_CHECK_EQUAL(result.front().getName(), "/zXxBth97ee");
412 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000413 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000414 this->advanceClocks(time::milliseconds(500));
415
416 RibEntry payload1;
417 payload1.setName("/zXxBth97ee");
418 RibEntry payload2;
419 payload2.setName("/rJ8CvUpr4G");
420 this->sendDataset("/localhost/nfd/rib/list", payload1, payload2);
421 this->advanceClocks(time::milliseconds(500));
422
423 BOOST_CHECK(hasResult);
424 BOOST_CHECK_EQUAL(failCodes.size(), 0);
425}
426
427BOOST_AUTO_TEST_CASE(RibListWithOptions)
428{
429 CommandOptions options;
430 options.setPrefix("/localhop/nfd");
431 bool hasResult = false;
432 controller.fetch<RibDataset>(
433 [&hasResult] (const std::vector<RibEntry>& result) {
434 hasResult = true;
435 BOOST_CHECK_EQUAL(result.size(), 1);
436 BOOST_CHECK_EQUAL(result.front().getName(), "/e6L5K4ascd");
437 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000438 datasetFailCallback,
Junxiao Shi034c1882016-06-24 18:06:51 +0000439 options);
440 this->advanceClocks(time::milliseconds(500));
441
442 RibEntry payload;
443 payload.setName("/e6L5K4ascd");
444 this->sendDataset("/localhop/nfd/rib/list", payload);
445 this->advanceClocks(time::milliseconds(500));
446
447 BOOST_CHECK(hasResult);
448 BOOST_CHECK_EQUAL(failCodes.size(), 0);
449}
450
451BOOST_AUTO_TEST_SUITE_END() // Datasets
452
Junxiao Shi7357ef22016-09-07 02:39:37 +0000453BOOST_AUTO_TEST_SUITE_END() // TestStatusDataset
454BOOST_AUTO_TEST_SUITE_END() // Nfd
455BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shi034c1882016-06-24 18:06:51 +0000456
457} // namespace tests
458} // namespace nfd
459} // namespace ndn