blob: e2f62a3cc019a397f2f7e4e2a6923f006a1696b6 [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
22#include "management/nfd-status-dataset.hpp"
23#include "management/nfd-controller.hpp"
24
25#include "nfd-controller-fixture.hpp"
26#include "../make-interest-data.hpp"
27
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
34BOOST_AUTO_TEST_SUITE(Management)
35
36class ControllerStatusDatasetFixture : public ControllerFixture
37{
38protected:
39 /** \brief send one WireEncodable as Data reply
40 * \param prefix dataset prefix without version and segment
41 * \param payload payload block
42 * \note payload must fit in one Data
43 */
44 template<typename T>
45 void
46 sendDataset(const Name& prefix, const T& payload)
47 {
48 BOOST_CONCEPT_ASSERT((WireEncodable<T>));
49
50 auto data = this->prepareDatasetReply(prefix);
51 data->setContent(payload.wireEncode());
52 face.receive(*signData(data));
53 }
54
55 /** \brief send two WireEncodables as Data reply
56 * \param prefix dataset prefix without version and segment
57 * \param payload1 first vector item
58 * \param payload2 second vector item
59 * \note all payloads must fit in one Data
60 */
61 template<typename T1, typename T2>
62 void
63 sendDataset(const Name& prefix, const T1& payload1, const T2& payload2)
64 {
65 // The test suite allows up to two items, and put them in the same Data packet,
66 // because this test suite focuses on Controller::fetch<StatusDataset>,
67 // and is not intended to cover SegmentFetcher behavior.
68
69 BOOST_CONCEPT_ASSERT((WireEncodable<T1>));
70 BOOST_CONCEPT_ASSERT((WireEncodable<T2>));
71
72 ndn::encoding::EncodingBuffer buffer;
73 payload2.wireEncode(buffer);
74 payload1.wireEncode(buffer);
75
76 auto data = this->prepareDatasetReply(prefix);
77 data->setContent(buffer.buf(), buffer.size());
78 face.receive(*signData(data));
79 }
80
81private:
82 shared_ptr<Data>
83 prepareDatasetReply(const Name& prefix)
84 {
85 Name name = prefix;
86 name.appendVersion().appendSegment(0);
87
88 // These warnings assist in debugging a `hasResult` check failure.
89 // They usually indicate a misspelled prefix or incorrect timing in the test case.
90 if (face.sentInterests.size() < 1) {
91 BOOST_WARN_MESSAGE(false, "no Interest expressed");
92 }
93 else {
94 BOOST_WARN_MESSAGE(face.sentInterests.back().getName().isPrefixOf(name),
95 "last Interest " << face.sentInterests.back().getName() <<
96 " cannot be satisfied by this Data " << name);
97 }
98
99 auto data = make_shared<Data>(name);
100 data->setFinalBlockId(data->getName()[-1]);
101 return data;
102 }
103};
104
105BOOST_FIXTURE_TEST_SUITE(TestNfdStatusDataset, ControllerStatusDatasetFixture)
106
107BOOST_AUTO_TEST_SUITE(Failures)
108
109BOOST_AUTO_TEST_CASE(Timeout)
110{
111 CommandOptions options;
112 options.setTimeout(time::milliseconds(3000));
113 controller.fetch<FaceDataset>(
114 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000115 datasetFailCallback,
Junxiao Shi034c1882016-06-24 18:06:51 +0000116 options);
117 this->advanceClocks(time::milliseconds(500), 7);
118
119 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
120 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_TIMEOUT);
121}
122
123BOOST_AUTO_TEST_CASE(DataHasNoSegment)
124{
125 controller.fetch<FaceDataset>(
126 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000127 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000128 this->advanceClocks(time::milliseconds(500));
129
130 face.receive(*makeData("/localhost/nfd/faces/list/%FD%00"));
131 this->advanceClocks(time::milliseconds(500));
132
133 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
134 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
135}
136
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000137BOOST_AUTO_TEST_CASE(ValidationFailure)
138{
139 this->setValidationResult(false);
140
141 controller.fetch<FaceDataset>(
142 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000143 datasetFailCallback);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000144 this->advanceClocks(time::milliseconds(500));
145
146 FaceStatus payload;
147 payload.setFaceId(5744);
148 this->sendDataset("/localhost/nfd/faces/list", payload);
149 this->advanceClocks(time::milliseconds(500));
150
151 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
152 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_VALIDATION);
153}
154
Junxiao Shi034c1882016-06-24 18:06:51 +0000155BOOST_AUTO_TEST_CASE(Nack)
156{
157 controller.fetch<FaceDataset>(
158 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000159 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000160 this->advanceClocks(time::milliseconds(500));
161
162 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
163 face.receive(lp::Nack(face.sentInterests.back()));
164 this->advanceClocks(time::milliseconds(500));
165
166 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
167 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_NACK);
168}
169
170BOOST_AUTO_TEST_CASE(ParseError1)
171{
172 controller.fetch<FaceDataset>(
173 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000174 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000175 this->advanceClocks(time::milliseconds(500));
176
177 Name payload; // Name is not valid FaceStatus
178 this->sendDataset("/localhost/nfd/faces/list", payload);
179 this->advanceClocks(time::milliseconds(500));
180
181 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
182 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
183}
184
185BOOST_AUTO_TEST_CASE(ParseError2)
186{
187 controller.fetch<FaceDataset>(
188 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000189 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000190 this->advanceClocks(time::milliseconds(500));
191
192 FaceStatus payload1;
193 payload1.setFaceId(10930);
194 Name payload2; // Name is not valid FaceStatus
195 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
196 this->advanceClocks(time::milliseconds(500));
197
198 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
199 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
200}
201
202BOOST_AUTO_TEST_SUITE_END() // Failures
203
Junxiao Shi600f7112016-07-16 11:57:18 +0000204BOOST_AUTO_TEST_SUITE(NoCallback)
205
206BOOST_AUTO_TEST_CASE(Success)
207{
208 controller.fetch<FaceDataset>(
209 nullptr,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000210 datasetFailCallback);
Junxiao Shi600f7112016-07-16 11:57:18 +0000211 this->advanceClocks(time::milliseconds(500));
212
213 FaceStatus payload;
214 payload.setFaceId(2577);
215 this->sendDataset("/localhost/nfd/faces/list", payload);
216 BOOST_CHECK_NO_THROW(this->advanceClocks(time::milliseconds(500)));
217
218 BOOST_CHECK_EQUAL(failCodes.size(), 0);
219}
220
221BOOST_AUTO_TEST_CASE(Failure)
222{
223 CommandOptions options;
224 options.setTimeout(time::milliseconds(3000));
225 controller.fetch<FaceDataset>(
226 [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
227 nullptr,
228 options);
229 BOOST_CHECK_NO_THROW(this->advanceClocks(time::milliseconds(500), 7));
230}
231
232BOOST_AUTO_TEST_SUITE_END() // NoCallback
233
Junxiao Shi034c1882016-06-24 18:06:51 +0000234BOOST_AUTO_TEST_SUITE(Datasets)
235
236BOOST_AUTO_TEST_CASE(StatusGeneral)
237{
238 bool hasResult = false;
239 controller.fetch<ForwarderGeneralStatusDataset>(
240 [&hasResult] (const ForwarderStatus& result) {
241 hasResult = true;
242 BOOST_CHECK_EQUAL(result.getNfdVersion(), "0.4.2");
243 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000244 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000245 this->advanceClocks(time::milliseconds(500));
246
247 ForwarderStatus payload;
248 payload.setNfdVersion("0.4.2");
249 this->sendDataset("/localhost/nfd/status/general", payload);
250 this->advanceClocks(time::milliseconds(500));
251
252 BOOST_CHECK(hasResult);
253 BOOST_CHECK_EQUAL(failCodes.size(), 0);
254}
255
256BOOST_AUTO_TEST_CASE(FaceList)
257{
258 bool hasResult = false;
259 controller.fetch<FaceDataset>(
260 [&hasResult] (const std::vector<FaceStatus>& result) {
261 hasResult = true;
262 BOOST_CHECK_EQUAL(result.size(), 2);
263 BOOST_CHECK_EQUAL(result.front().getFaceId(), 24485);
264 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000265 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000266 this->advanceClocks(time::milliseconds(500));
267
268 FaceStatus payload1;
269 payload1.setFaceId(24485);
270 FaceStatus payload2;
271 payload2.setFaceId(12987);
272 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
273 this->advanceClocks(time::milliseconds(500));
274
275 BOOST_CHECK(hasResult);
276 BOOST_CHECK_EQUAL(failCodes.size(), 0);
277}
278
279BOOST_AUTO_TEST_CASE(FaceQuery)
280{
281 FaceQueryFilter filter;
282 filter.setUriScheme("udp4");
283 bool hasResult = false;
284 controller.fetch<FaceQueryDataset>(
285 filter,
286 [&hasResult] (const std::vector<FaceStatus>& result) {
287 hasResult = true;
288 BOOST_CHECK_EQUAL(result.size(), 1);
289 BOOST_CHECK_EQUAL(result.front().getFaceId(), 8795);
290 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000291 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000292 this->advanceClocks(time::milliseconds(500));
293
294 Name prefix("/localhost/nfd/faces/query");
295 prefix.append(filter.wireEncode());
296 FaceStatus payload;
297 payload.setFaceId(8795);
298 this->sendDataset(prefix, payload);
299 this->advanceClocks(time::milliseconds(500));
300
301 BOOST_CHECK(hasResult);
302 BOOST_CHECK_EQUAL(failCodes.size(), 0);
303}
304
305BOOST_AUTO_TEST_CASE(FaceQueryWithOptions)
306{
307 FaceQueryFilter filter;
308 filter.setUriScheme("udp4");
309 CommandOptions options;
310 options.setTimeout(time::milliseconds(3000));
311 bool hasResult = false;
312 controller.fetch<FaceQueryDataset>(
313 filter,
314 [&hasResult] (const std::vector<FaceStatus>& result) {
315 hasResult = true;
316 BOOST_CHECK_EQUAL(result.size(), 1);
317 BOOST_CHECK_EQUAL(result.front().getFaceId(), 14022);
318 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000319 datasetFailCallback,
Junxiao Shi034c1882016-06-24 18:06:51 +0000320 options);
321 this->advanceClocks(time::milliseconds(500));
322
323 Name prefix("/localhost/nfd/faces/query");
324 prefix.append(filter.wireEncode());
325 FaceStatus payload;
326 payload.setFaceId(14022);
327 this->sendDataset(prefix, payload);
328 this->advanceClocks(time::milliseconds(500));
329
330 BOOST_CHECK(hasResult);
331 BOOST_CHECK_EQUAL(failCodes.size(), 0);
332}
333
Junxiao Shi1eacb972016-07-07 22:51:07 +0000334BOOST_AUTO_TEST_CASE(FaceChannels)
335{
336 bool hasResult = false;
337 controller.fetch<ChannelDataset>(
338 [&hasResult] (const std::vector<ChannelStatus>& result) {
339 hasResult = true;
340 BOOST_CHECK_EQUAL(result.size(), 2);
341 BOOST_CHECK_EQUAL(result.front().getLocalUri(), "tcp4://192.0.2.1:6363");
342 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000343 datasetFailCallback);
Junxiao Shi1eacb972016-07-07 22:51:07 +0000344 this->advanceClocks(time::milliseconds(500));
345
346 ChannelStatus payload1;
347 payload1.setLocalUri("tcp4://192.0.2.1:6363");
348 ChannelStatus payload2;
349 payload2.setLocalUri("udp4://192.0.2.1:6363");
350 this->sendDataset("/localhost/nfd/faces/channels", payload1, payload2);
351 this->advanceClocks(time::milliseconds(500));
352
353 BOOST_CHECK(hasResult);
354 BOOST_CHECK_EQUAL(failCodes.size(), 0);
355}
356
Junxiao Shi034c1882016-06-24 18:06:51 +0000357BOOST_AUTO_TEST_CASE(FibList)
358{
359 bool hasResult = false;
360 controller.fetch<FibDataset>(
361 [&hasResult] (const std::vector<FibEntry>& result) {
362 hasResult = true;
363 BOOST_CHECK_EQUAL(result.size(), 2);
364 BOOST_CHECK_EQUAL(result.front().getPrefix(), "/wYs7fzYcfG");
365 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000366 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000367 this->advanceClocks(time::milliseconds(500));
368
369 FibEntry payload1;
370 payload1.setPrefix("/wYs7fzYcfG");
371 FibEntry payload2;
372 payload2.setPrefix("/LKvmnzY5S");
373 this->sendDataset("/localhost/nfd/fib/list", payload1, payload2);
374 this->advanceClocks(time::milliseconds(500));
375
376 BOOST_CHECK(hasResult);
377 BOOST_CHECK_EQUAL(failCodes.size(), 0);
378}
379
380BOOST_AUTO_TEST_CASE(StrategyChoiceList)
381{
382 bool hasResult = false;
383 controller.fetch<StrategyChoiceDataset>(
384 [&hasResult] (const std::vector<StrategyChoice>& result) {
385 hasResult = true;
386 BOOST_CHECK_EQUAL(result.size(), 2);
387 BOOST_CHECK_EQUAL(result.front().getName(), "/8MLz6N3B");
388 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000389 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000390 this->advanceClocks(time::milliseconds(500));
391
392 StrategyChoice payload1;
393 payload1.setName("/8MLz6N3B");
394 StrategyChoice payload2;
395 payload2.setName("/svqcBu0YwU");
396 this->sendDataset("/localhost/nfd/strategy-choice/list", payload1, payload2);
397 this->advanceClocks(time::milliseconds(500));
398
399 BOOST_CHECK(hasResult);
400 BOOST_CHECK_EQUAL(failCodes.size(), 0);
401}
402
403BOOST_AUTO_TEST_CASE(RibList)
404{
405 bool hasResult = false;
406 controller.fetch<RibDataset>(
407 [&hasResult] (const std::vector<RibEntry>& result) {
408 hasResult = true;
409 BOOST_CHECK_EQUAL(result.size(), 2);
410 BOOST_CHECK_EQUAL(result.front().getName(), "/zXxBth97ee");
411 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000412 datasetFailCallback);
Junxiao Shi034c1882016-06-24 18:06:51 +0000413 this->advanceClocks(time::milliseconds(500));
414
415 RibEntry payload1;
416 payload1.setName("/zXxBth97ee");
417 RibEntry payload2;
418 payload2.setName("/rJ8CvUpr4G");
419 this->sendDataset("/localhost/nfd/rib/list", payload1, payload2);
420 this->advanceClocks(time::milliseconds(500));
421
422 BOOST_CHECK(hasResult);
423 BOOST_CHECK_EQUAL(failCodes.size(), 0);
424}
425
426BOOST_AUTO_TEST_CASE(RibListWithOptions)
427{
428 CommandOptions options;
429 options.setPrefix("/localhop/nfd");
430 bool hasResult = false;
431 controller.fetch<RibDataset>(
432 [&hasResult] (const std::vector<RibEntry>& result) {
433 hasResult = true;
434 BOOST_CHECK_EQUAL(result.size(), 1);
435 BOOST_CHECK_EQUAL(result.front().getName(), "/e6L5K4ascd");
436 },
Junxiao Shie7c7f152016-08-20 22:36:22 +0000437 datasetFailCallback,
Junxiao Shi034c1882016-06-24 18:06:51 +0000438 options);
439 this->advanceClocks(time::milliseconds(500));
440
441 RibEntry payload;
442 payload.setName("/e6L5K4ascd");
443 this->sendDataset("/localhop/nfd/rib/list", payload);
444 this->advanceClocks(time::milliseconds(500));
445
446 BOOST_CHECK(hasResult);
447 BOOST_CHECK_EQUAL(failCodes.size(), 0);
448}
449
450BOOST_AUTO_TEST_SUITE_END() // Datasets
451
452BOOST_AUTO_TEST_SUITE_END() // TestNfdStatusDataset
453BOOST_AUTO_TEST_SUITE_END() // Management
454
455} // namespace tests
456} // namespace nfd
457} // namespace ndn