Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 1 | /* -*- 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 | |
| 28 | namespace ndn { |
| 29 | namespace nfd { |
| 30 | namespace tests { |
| 31 | |
| 32 | using ndn::util::makeData; |
| 33 | using ndn::util::signData; |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(Management) |
| 36 | |
| 37 | class ControllerStatusDatasetFixture : public ControllerFixture |
| 38 | { |
| 39 | protected: |
| 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 | |
| 82 | private: |
| 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 | |
| 106 | BOOST_FIXTURE_TEST_SUITE(TestNfdStatusDataset, ControllerStatusDatasetFixture) |
| 107 | |
| 108 | BOOST_AUTO_TEST_SUITE(Failures) |
| 109 | |
| 110 | BOOST_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"); }, |
| 116 | failCallback, |
| 117 | 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 | |
| 124 | BOOST_AUTO_TEST_CASE(DataHasNoSegment) |
| 125 | { |
| 126 | controller.fetch<FaceDataset>( |
| 127 | [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); }, |
| 128 | failCallback); |
| 129 | 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 Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 138 | BOOST_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"); }, |
| 144 | failCallback); |
| 145 | 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 Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 156 | BOOST_AUTO_TEST_CASE(Nack) |
| 157 | { |
| 158 | controller.fetch<FaceDataset>( |
| 159 | [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); }, |
| 160 | failCallback); |
| 161 | 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 | |
| 171 | BOOST_AUTO_TEST_CASE(ParseError1) |
| 172 | { |
| 173 | controller.fetch<FaceDataset>( |
| 174 | [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); }, |
| 175 | failCallback); |
| 176 | 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 | |
| 186 | BOOST_AUTO_TEST_CASE(ParseError2) |
| 187 | { |
| 188 | controller.fetch<FaceDataset>( |
| 189 | [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); }, |
| 190 | failCallback); |
| 191 | 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 | |
| 203 | BOOST_AUTO_TEST_SUITE_END() // Failures |
| 204 | |
| 205 | BOOST_AUTO_TEST_SUITE(Datasets) |
| 206 | |
| 207 | BOOST_AUTO_TEST_CASE(StatusGeneral) |
| 208 | { |
| 209 | bool hasResult = false; |
| 210 | controller.fetch<ForwarderGeneralStatusDataset>( |
| 211 | [&hasResult] (const ForwarderStatus& result) { |
| 212 | hasResult = true; |
| 213 | BOOST_CHECK_EQUAL(result.getNfdVersion(), "0.4.2"); |
| 214 | }, |
| 215 | failCallback); |
| 216 | this->advanceClocks(time::milliseconds(500)); |
| 217 | |
| 218 | ForwarderStatus payload; |
| 219 | payload.setNfdVersion("0.4.2"); |
| 220 | this->sendDataset("/localhost/nfd/status/general", payload); |
| 221 | this->advanceClocks(time::milliseconds(500)); |
| 222 | |
| 223 | BOOST_CHECK(hasResult); |
| 224 | BOOST_CHECK_EQUAL(failCodes.size(), 0); |
| 225 | } |
| 226 | |
| 227 | BOOST_AUTO_TEST_CASE(FaceList) |
| 228 | { |
| 229 | bool hasResult = false; |
| 230 | controller.fetch<FaceDataset>( |
| 231 | [&hasResult] (const std::vector<FaceStatus>& result) { |
| 232 | hasResult = true; |
| 233 | BOOST_CHECK_EQUAL(result.size(), 2); |
| 234 | BOOST_CHECK_EQUAL(result.front().getFaceId(), 24485); |
| 235 | }, |
| 236 | failCallback); |
| 237 | this->advanceClocks(time::milliseconds(500)); |
| 238 | |
| 239 | FaceStatus payload1; |
| 240 | payload1.setFaceId(24485); |
| 241 | FaceStatus payload2; |
| 242 | payload2.setFaceId(12987); |
| 243 | this->sendDataset("/localhost/nfd/faces/list", payload1, payload2); |
| 244 | this->advanceClocks(time::milliseconds(500)); |
| 245 | |
| 246 | BOOST_CHECK(hasResult); |
| 247 | BOOST_CHECK_EQUAL(failCodes.size(), 0); |
| 248 | } |
| 249 | |
| 250 | BOOST_AUTO_TEST_CASE(FaceQuery) |
| 251 | { |
| 252 | FaceQueryFilter filter; |
| 253 | filter.setUriScheme("udp4"); |
| 254 | bool hasResult = false; |
| 255 | controller.fetch<FaceQueryDataset>( |
| 256 | filter, |
| 257 | [&hasResult] (const std::vector<FaceStatus>& result) { |
| 258 | hasResult = true; |
| 259 | BOOST_CHECK_EQUAL(result.size(), 1); |
| 260 | BOOST_CHECK_EQUAL(result.front().getFaceId(), 8795); |
| 261 | }, |
| 262 | failCallback); |
| 263 | this->advanceClocks(time::milliseconds(500)); |
| 264 | |
| 265 | Name prefix("/localhost/nfd/faces/query"); |
| 266 | prefix.append(filter.wireEncode()); |
| 267 | FaceStatus payload; |
| 268 | payload.setFaceId(8795); |
| 269 | this->sendDataset(prefix, payload); |
| 270 | this->advanceClocks(time::milliseconds(500)); |
| 271 | |
| 272 | BOOST_CHECK(hasResult); |
| 273 | BOOST_CHECK_EQUAL(failCodes.size(), 0); |
| 274 | } |
| 275 | |
| 276 | BOOST_AUTO_TEST_CASE(FaceQueryWithOptions) |
| 277 | { |
| 278 | FaceQueryFilter filter; |
| 279 | filter.setUriScheme("udp4"); |
| 280 | CommandOptions options; |
| 281 | options.setTimeout(time::milliseconds(3000)); |
| 282 | bool hasResult = false; |
| 283 | controller.fetch<FaceQueryDataset>( |
| 284 | filter, |
| 285 | [&hasResult] (const std::vector<FaceStatus>& result) { |
| 286 | hasResult = true; |
| 287 | BOOST_CHECK_EQUAL(result.size(), 1); |
| 288 | BOOST_CHECK_EQUAL(result.front().getFaceId(), 14022); |
| 289 | }, |
| 290 | failCallback, |
| 291 | options); |
| 292 | this->advanceClocks(time::milliseconds(500)); |
| 293 | |
| 294 | Name prefix("/localhost/nfd/faces/query"); |
| 295 | prefix.append(filter.wireEncode()); |
| 296 | FaceStatus payload; |
| 297 | payload.setFaceId(14022); |
| 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 | |
Junxiao Shi | 1eacb97 | 2016-07-07 22:51:07 +0000 | [diff] [blame] | 305 | BOOST_AUTO_TEST_CASE(FaceChannels) |
| 306 | { |
| 307 | bool hasResult = false; |
| 308 | controller.fetch<ChannelDataset>( |
| 309 | [&hasResult] (const std::vector<ChannelStatus>& result) { |
| 310 | hasResult = true; |
| 311 | BOOST_CHECK_EQUAL(result.size(), 2); |
| 312 | BOOST_CHECK_EQUAL(result.front().getLocalUri(), "tcp4://192.0.2.1:6363"); |
| 313 | }, |
| 314 | failCallback); |
| 315 | this->advanceClocks(time::milliseconds(500)); |
| 316 | |
| 317 | ChannelStatus payload1; |
| 318 | payload1.setLocalUri("tcp4://192.0.2.1:6363"); |
| 319 | ChannelStatus payload2; |
| 320 | payload2.setLocalUri("udp4://192.0.2.1:6363"); |
| 321 | this->sendDataset("/localhost/nfd/faces/channels", payload1, payload2); |
| 322 | this->advanceClocks(time::milliseconds(500)); |
| 323 | |
| 324 | BOOST_CHECK(hasResult); |
| 325 | BOOST_CHECK_EQUAL(failCodes.size(), 0); |
| 326 | } |
| 327 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 328 | BOOST_AUTO_TEST_CASE(FibList) |
| 329 | { |
| 330 | bool hasResult = false; |
| 331 | controller.fetch<FibDataset>( |
| 332 | [&hasResult] (const std::vector<FibEntry>& result) { |
| 333 | hasResult = true; |
| 334 | BOOST_CHECK_EQUAL(result.size(), 2); |
| 335 | BOOST_CHECK_EQUAL(result.front().getPrefix(), "/wYs7fzYcfG"); |
| 336 | }, |
| 337 | failCallback); |
| 338 | this->advanceClocks(time::milliseconds(500)); |
| 339 | |
| 340 | FibEntry payload1; |
| 341 | payload1.setPrefix("/wYs7fzYcfG"); |
| 342 | FibEntry payload2; |
| 343 | payload2.setPrefix("/LKvmnzY5S"); |
| 344 | this->sendDataset("/localhost/nfd/fib/list", payload1, payload2); |
| 345 | this->advanceClocks(time::milliseconds(500)); |
| 346 | |
| 347 | BOOST_CHECK(hasResult); |
| 348 | BOOST_CHECK_EQUAL(failCodes.size(), 0); |
| 349 | } |
| 350 | |
| 351 | BOOST_AUTO_TEST_CASE(StrategyChoiceList) |
| 352 | { |
| 353 | bool hasResult = false; |
| 354 | controller.fetch<StrategyChoiceDataset>( |
| 355 | [&hasResult] (const std::vector<StrategyChoice>& result) { |
| 356 | hasResult = true; |
| 357 | BOOST_CHECK_EQUAL(result.size(), 2); |
| 358 | BOOST_CHECK_EQUAL(result.front().getName(), "/8MLz6N3B"); |
| 359 | }, |
| 360 | failCallback); |
| 361 | this->advanceClocks(time::milliseconds(500)); |
| 362 | |
| 363 | StrategyChoice payload1; |
| 364 | payload1.setName("/8MLz6N3B"); |
| 365 | StrategyChoice payload2; |
| 366 | payload2.setName("/svqcBu0YwU"); |
| 367 | this->sendDataset("/localhost/nfd/strategy-choice/list", payload1, payload2); |
| 368 | this->advanceClocks(time::milliseconds(500)); |
| 369 | |
| 370 | BOOST_CHECK(hasResult); |
| 371 | BOOST_CHECK_EQUAL(failCodes.size(), 0); |
| 372 | } |
| 373 | |
| 374 | BOOST_AUTO_TEST_CASE(RibList) |
| 375 | { |
| 376 | bool hasResult = false; |
| 377 | controller.fetch<RibDataset>( |
| 378 | [&hasResult] (const std::vector<RibEntry>& result) { |
| 379 | hasResult = true; |
| 380 | BOOST_CHECK_EQUAL(result.size(), 2); |
| 381 | BOOST_CHECK_EQUAL(result.front().getName(), "/zXxBth97ee"); |
| 382 | }, |
| 383 | failCallback); |
| 384 | this->advanceClocks(time::milliseconds(500)); |
| 385 | |
| 386 | RibEntry payload1; |
| 387 | payload1.setName("/zXxBth97ee"); |
| 388 | RibEntry payload2; |
| 389 | payload2.setName("/rJ8CvUpr4G"); |
| 390 | this->sendDataset("/localhost/nfd/rib/list", payload1, payload2); |
| 391 | this->advanceClocks(time::milliseconds(500)); |
| 392 | |
| 393 | BOOST_CHECK(hasResult); |
| 394 | BOOST_CHECK_EQUAL(failCodes.size(), 0); |
| 395 | } |
| 396 | |
| 397 | BOOST_AUTO_TEST_CASE(RibListWithOptions) |
| 398 | { |
| 399 | CommandOptions options; |
| 400 | options.setPrefix("/localhop/nfd"); |
| 401 | bool hasResult = false; |
| 402 | controller.fetch<RibDataset>( |
| 403 | [&hasResult] (const std::vector<RibEntry>& result) { |
| 404 | hasResult = true; |
| 405 | BOOST_CHECK_EQUAL(result.size(), 1); |
| 406 | BOOST_CHECK_EQUAL(result.front().getName(), "/e6L5K4ascd"); |
| 407 | }, |
| 408 | failCallback, |
| 409 | options); |
| 410 | this->advanceClocks(time::milliseconds(500)); |
| 411 | |
| 412 | RibEntry payload; |
| 413 | payload.setName("/e6L5K4ascd"); |
| 414 | this->sendDataset("/localhop/nfd/rib/list", payload); |
| 415 | this->advanceClocks(time::milliseconds(500)); |
| 416 | |
| 417 | BOOST_CHECK(hasResult); |
| 418 | BOOST_CHECK_EQUAL(failCodes.size(), 0); |
| 419 | } |
| 420 | |
| 421 | BOOST_AUTO_TEST_SUITE_END() // Datasets |
| 422 | |
| 423 | BOOST_AUTO_TEST_SUITE_END() // TestNfdStatusDataset |
| 424 | BOOST_AUTO_TEST_SUITE_END() // Management |
| 425 | |
| 426 | } // namespace tests |
| 427 | } // namespace nfd |
| 428 | } // namespace ndn |