Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1 | // /* -*- 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/face-manager.hpp" |
| 8 | #include "mgmt/internal-face.hpp" |
| 9 | #include "face/face.hpp" |
| 10 | #include "../face/dummy-face.hpp" |
| 11 | #include "fw/face-table.hpp" |
| 12 | #include "fw/forwarder.hpp" |
| 13 | |
| 14 | #include "common.hpp" |
| 15 | #include "tests/test-common.hpp" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 16 | #include "validation-common.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 17 | |
| 18 | namespace nfd { |
| 19 | namespace tests { |
| 20 | |
| 21 | NFD_LOG_INIT("FaceManagerTest"); |
| 22 | |
| 23 | class TestDummyFace : public DummyFace |
| 24 | { |
| 25 | public: |
| 26 | |
| 27 | TestDummyFace() |
| 28 | : m_closeFired(false) |
| 29 | { |
| 30 | |
| 31 | } |
| 32 | |
| 33 | virtual |
| 34 | ~TestDummyFace() |
| 35 | { |
| 36 | |
| 37 | } |
| 38 | |
| 39 | virtual void |
| 40 | close() |
| 41 | { |
| 42 | m_closeFired = true; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | didCloseFire() const |
| 47 | { |
| 48 | return m_closeFired; |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | bool m_closeFired; |
| 53 | }; |
| 54 | |
| 55 | class TestFaceTable : public FaceTable |
| 56 | { |
| 57 | public: |
| 58 | TestFaceTable(Forwarder& forwarder) |
| 59 | : FaceTable(forwarder), |
| 60 | m_addFired(false), |
| 61 | m_removeFired(false), |
| 62 | m_getFired(false), |
| 63 | m_dummy(make_shared<TestDummyFace>()) |
| 64 | { |
| 65 | |
| 66 | } |
| 67 | |
| 68 | virtual |
| 69 | ~TestFaceTable() |
| 70 | { |
| 71 | |
| 72 | } |
| 73 | |
| 74 | virtual void |
| 75 | add(shared_ptr<Face> face) |
| 76 | { |
| 77 | m_addFired = true; |
| 78 | } |
| 79 | |
| 80 | virtual void |
| 81 | remove(shared_ptr<Face> face) |
| 82 | { |
| 83 | m_removeFired = true; |
| 84 | } |
| 85 | |
| 86 | virtual shared_ptr<Face> |
| 87 | get(FaceId id) const |
| 88 | { |
| 89 | m_getFired = true; |
| 90 | return m_dummy; |
| 91 | } |
| 92 | |
| 93 | bool |
| 94 | didAddFire() const |
| 95 | { |
| 96 | return m_addFired; |
| 97 | } |
| 98 | |
| 99 | bool |
| 100 | didRemoveFire() const |
| 101 | { |
| 102 | return m_removeFired; |
| 103 | } |
| 104 | |
| 105 | bool |
| 106 | didGetFire() const |
| 107 | { |
| 108 | return m_getFired; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | reset() |
| 113 | { |
| 114 | m_addFired = false; |
| 115 | m_removeFired = false; |
| 116 | m_getFired = false; |
| 117 | } |
| 118 | |
| 119 | shared_ptr<TestDummyFace>& |
| 120 | getDummyFace() |
| 121 | { |
| 122 | return m_dummy; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | bool m_addFired; |
| 127 | bool m_removeFired; |
| 128 | mutable bool m_getFired; |
| 129 | shared_ptr<TestDummyFace> m_dummy; |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | class TestFaceTableFixture : public BaseFixture |
| 134 | { |
| 135 | public: |
| 136 | TestFaceTableFixture() |
| 137 | : m_faceTable(m_forwarder) |
| 138 | { |
| 139 | |
| 140 | } |
| 141 | |
| 142 | virtual |
| 143 | ~TestFaceTableFixture() |
| 144 | { |
| 145 | |
| 146 | } |
| 147 | |
| 148 | protected: |
| 149 | Forwarder m_forwarder; |
| 150 | TestFaceTable m_faceTable; |
| 151 | }; |
| 152 | |
| 153 | class TestFaceManagerCommon |
| 154 | { |
| 155 | public: |
| 156 | TestFaceManagerCommon() |
| 157 | : m_face(make_shared<InternalFace>()), |
| 158 | m_callbackFired(false) |
| 159 | { |
| 160 | |
| 161 | } |
| 162 | |
| 163 | virtual |
| 164 | ~TestFaceManagerCommon() |
| 165 | { |
| 166 | |
| 167 | } |
| 168 | |
| 169 | shared_ptr<InternalFace>& |
| 170 | getFace() |
| 171 | { |
| 172 | return m_face; |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | validateControlResponseCommon(const Data& response, |
| 177 | const Name& expectedName, |
| 178 | uint32_t expectedCode, |
| 179 | const std::string& expectedText, |
| 180 | ControlResponse& control) |
| 181 | { |
| 182 | m_callbackFired = true; |
| 183 | Block controlRaw = response.getContent().blockFromValue(); |
| 184 | |
| 185 | control.wireDecode(controlRaw); |
| 186 | |
| 187 | // NFD_LOG_DEBUG("received control response" |
| 188 | // << " Name: " << response.getName() |
| 189 | // << " code: " << control.getCode() |
| 190 | // << " text: " << control.getText()); |
| 191 | |
| 192 | BOOST_CHECK_EQUAL(response.getName(), expectedName); |
| 193 | BOOST_CHECK_EQUAL(control.getCode(), expectedCode); |
| 194 | BOOST_CHECK_EQUAL(control.getText(), expectedText); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | validateControlResponse(const Data& response, |
| 199 | const Name& expectedName, |
| 200 | uint32_t expectedCode, |
| 201 | const std::string& expectedText) |
| 202 | { |
| 203 | ControlResponse control; |
| 204 | validateControlResponseCommon(response, expectedName, |
| 205 | expectedCode, expectedText, control); |
| 206 | |
| 207 | if (!control.getBody().empty()) |
| 208 | { |
| 209 | BOOST_FAIL("found unexpected control response body"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | validateControlResponse(const Data& response, |
| 215 | const Name& expectedName, |
| 216 | uint32_t expectedCode, |
| 217 | const std::string& expectedText, |
| 218 | const Block& expectedBody) |
| 219 | { |
| 220 | ControlResponse control; |
| 221 | validateControlResponseCommon(response, expectedName, |
| 222 | expectedCode, expectedText, control); |
| 223 | |
| 224 | BOOST_REQUIRE(!control.getBody().empty()); |
| 225 | BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size()); |
| 226 | |
| 227 | BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(), |
| 228 | expectedBody.value_size()) == 0); |
| 229 | |
| 230 | } |
| 231 | |
| 232 | bool |
| 233 | didCallbackFire() const |
| 234 | { |
| 235 | return m_callbackFired; |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | resetCallbackFired() |
| 240 | { |
| 241 | m_callbackFired = false; |
| 242 | } |
| 243 | |
| 244 | protected: |
| 245 | shared_ptr<InternalFace> m_face; |
| 246 | |
| 247 | private: |
| 248 | bool m_callbackFired; |
| 249 | }; |
| 250 | |
| 251 | class FaceManagerFixture : public TestFaceTableFixture, public TestFaceManagerCommon |
| 252 | { |
| 253 | public: |
| 254 | FaceManagerFixture() |
| 255 | : m_manager(m_faceTable, m_face) |
| 256 | { |
| 257 | m_manager.setConfigFile(m_config); |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | parseConfig(const std::string configuration, bool isDryRun) |
| 262 | { |
| 263 | m_config.parse(configuration, isDryRun); |
| 264 | } |
| 265 | |
| 266 | virtual |
| 267 | ~FaceManagerFixture() |
| 268 | { |
| 269 | |
| 270 | } |
| 271 | |
| 272 | FaceManager& |
| 273 | getManager() |
| 274 | { |
| 275 | return m_manager; |
| 276 | } |
| 277 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 278 | void |
| 279 | addInterestRule(const std::string& regex, |
| 280 | ndn::IdentityCertificate& certificate) |
| 281 | { |
| 282 | m_manager.addInterestRule(regex, certificate); |
| 283 | } |
| 284 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 285 | bool |
| 286 | didFaceTableAddFire() const |
| 287 | { |
| 288 | return m_faceTable.didAddFire(); |
| 289 | } |
| 290 | |
| 291 | bool |
| 292 | didFaceTableRemoveFire() const |
| 293 | { |
| 294 | return m_faceTable.didRemoveFire(); |
| 295 | } |
| 296 | |
| 297 | bool |
| 298 | didFaceTableGetFire() const |
| 299 | { |
| 300 | return m_faceTable.didGetFire(); |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | resetFaceTable() |
| 305 | { |
| 306 | m_faceTable.reset(); |
| 307 | } |
| 308 | |
| 309 | private: |
| 310 | FaceManager m_manager; |
| 311 | ConfigFile m_config; |
| 312 | }; |
| 313 | |
| 314 | BOOST_FIXTURE_TEST_SUITE(MgmtFaceManager, FaceManagerFixture) |
| 315 | |
| 316 | bool |
| 317 | isExpectedException(const ConfigFile::Error& error, const std::string& expectedMessage) |
| 318 | { |
| 319 | if (error.what() != expectedMessage) |
| 320 | { |
| 321 | NFD_LOG_ERROR("expected: " << expectedMessage << "\tgot: " << error.what()); |
| 322 | } |
| 323 | return error.what() == expectedMessage; |
| 324 | } |
| 325 | |
| 326 | BOOST_AUTO_TEST_CASE(TestProcessSectionUnix) |
| 327 | { |
| 328 | const std::string CONFIG = |
| 329 | "face_system\n" |
| 330 | "{\n" |
| 331 | " unix\n" |
| 332 | " {\n" |
| 333 | " listen yes\n" |
| 334 | " path /tmp/nfd.sock\n" |
| 335 | " }\n" |
| 336 | "}\n"; |
| 337 | BOOST_TEST_CHECKPOINT("Calling parse"); |
| 338 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false)); |
| 339 | } |
| 340 | |
| 341 | BOOST_AUTO_TEST_CASE(TestProcessSectionUnixDryRun) |
| 342 | { |
| 343 | const std::string CONFIG = |
| 344 | "face_system\n" |
| 345 | "{\n" |
| 346 | " unix\n" |
| 347 | " {\n" |
| 348 | " listen yes\n" |
| 349 | " path /var/run/nfd.sock\n" |
| 350 | " }\n" |
| 351 | "}\n"; |
| 352 | |
| 353 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true)); |
| 354 | } |
| 355 | |
| 356 | BOOST_AUTO_TEST_CASE(TestProcessSectionUnixBadListen) |
| 357 | { |
| 358 | const std::string CONFIG = |
| 359 | "face_system\n" |
| 360 | "{\n" |
| 361 | " unix\n" |
| 362 | " {\n" |
| 363 | " listen hello\n" |
| 364 | " }\n" |
| 365 | "}\n"; |
| 366 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 367 | bind(&isExpectedException, _1, |
| 368 | "Invalid value for option \"listen\" in \"unix\" section")); |
| 369 | } |
| 370 | |
| 371 | BOOST_AUTO_TEST_CASE(TestProcessSectionUnixUnknownOption) |
| 372 | { |
| 373 | const std::string CONFIG = |
| 374 | "face_system\n" |
| 375 | "{\n" |
| 376 | " unix\n" |
| 377 | " {\n" |
| 378 | " hello\n" |
| 379 | " }\n" |
| 380 | "}\n"; |
| 381 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 382 | bind(&isExpectedException, _1, |
| 383 | "Unrecognized option \"hello\" in \"unix\" section")); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
| 388 | BOOST_AUTO_TEST_CASE(TestProcessSectionTcp) |
| 389 | { |
| 390 | const std::string CONFIG = |
| 391 | "face_system\n" |
| 392 | "{\n" |
| 393 | " tcp\n" |
| 394 | " {\n" |
| 395 | " listen yes\n" |
| 396 | " port 6363\n" |
| 397 | " }\n" |
| 398 | "}\n"; |
| 399 | try |
| 400 | { |
| 401 | parseConfig(CONFIG, false); |
| 402 | } |
| 403 | catch (const std::runtime_error& e) |
| 404 | { |
| 405 | const std::string reason = e.what(); |
| 406 | if (reason.find("Address in use") != std::string::npos) |
| 407 | { |
| 408 | BOOST_FAIL(reason); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | BOOST_AUTO_TEST_CASE(TestProcessSectionTcpDryRun) |
| 414 | { |
| 415 | const std::string CONFIG = |
| 416 | "face_system\n" |
| 417 | "{\n" |
| 418 | " tcp\n" |
| 419 | " {\n" |
| 420 | " listen yes\n" |
| 421 | " port 6363\n" |
| 422 | " }\n" |
| 423 | "}\n"; |
| 424 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true)); |
| 425 | } |
| 426 | |
| 427 | BOOST_AUTO_TEST_CASE(TestProcessSectionTcpBadListen) |
| 428 | { |
| 429 | const std::string CONFIG = |
| 430 | "face_system\n" |
| 431 | "{\n" |
| 432 | " tcp\n" |
| 433 | " {\n" |
| 434 | " listen hello\n" |
| 435 | " }\n" |
| 436 | "}\n"; |
| 437 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 438 | bind(&isExpectedException, _1, |
| 439 | "Invalid value for option \"listen\" in \"tcp\" section")); |
| 440 | } |
| 441 | |
| 442 | BOOST_AUTO_TEST_CASE(TestProcessSectionTcpUnknownOption) |
| 443 | { |
| 444 | const std::string CONFIG = |
| 445 | "face_system\n" |
| 446 | "{\n" |
| 447 | " tcp\n" |
| 448 | " {\n" |
| 449 | " hello\n" |
| 450 | " }\n" |
| 451 | "}\n"; |
| 452 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 453 | bind(&isExpectedException, _1, |
| 454 | "Unrecognized option \"hello\" in \"tcp\" section")); |
| 455 | } |
| 456 | |
| 457 | BOOST_AUTO_TEST_CASE(TestProcessSectionUdp) |
| 458 | { |
| 459 | const std::string CONFIG = |
| 460 | "face_system\n" |
| 461 | "{\n" |
| 462 | " udp\n" |
| 463 | " {\n" |
| 464 | " port 6363\n" |
| 465 | " idle_timeout 30\n" |
| 466 | " keep_alive_interval 25\n" |
| 467 | " mcast yes\n" |
| 468 | " mcast_port 56363\n" |
| 469 | " mcast_group 224.0.23.170\n" |
| 470 | " }\n" |
| 471 | "}\n"; |
| 472 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false)); |
| 473 | } |
| 474 | |
| 475 | BOOST_AUTO_TEST_CASE(TestProcessSectionUdpDryRun) |
| 476 | { |
| 477 | const std::string CONFIG = |
| 478 | "face_system\n" |
| 479 | "{\n" |
| 480 | " udp\n" |
| 481 | " {\n" |
| 482 | " port 6363\n" |
| 483 | " idle_timeout 30\n" |
| 484 | " keep_alive_interval 25\n" |
| 485 | " mcast yes\n" |
| 486 | " mcast_port 56363\n" |
| 487 | " mcast_group 224.0.23.170\n" |
| 488 | " }\n" |
| 489 | "}\n"; |
| 490 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true)); |
| 491 | } |
| 492 | |
| 493 | BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadIdleTimeout) |
| 494 | { |
| 495 | const std::string CONFIG = |
| 496 | "face_system\n" |
| 497 | "{\n" |
| 498 | " udp\n" |
| 499 | " {\n" |
| 500 | " idle_timeout hello\n" |
| 501 | " }\n" |
| 502 | "}\n"; |
| 503 | |
| 504 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 505 | bind(&isExpectedException, _1, |
| 506 | "Invalid value for option \"idle_timeout\" in \"udp\" section")); |
| 507 | } |
| 508 | |
| 509 | BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcast) |
| 510 | { |
| 511 | const std::string CONFIG = |
| 512 | "face_system\n" |
| 513 | "{\n" |
| 514 | " udp\n" |
| 515 | " {\n" |
| 516 | " mcast hello\n" |
| 517 | " }\n" |
| 518 | "}\n"; |
| 519 | |
| 520 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 521 | bind(&isExpectedException, _1, |
| 522 | "Invalid value for option \"mcast\" in \"udp\" section")); |
| 523 | } |
| 524 | |
| 525 | BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcastGroup) |
| 526 | { |
| 527 | const std::string CONFIG = |
| 528 | "face_system\n" |
| 529 | "{\n" |
| 530 | " udp\n" |
| 531 | " {\n" |
| 532 | " mcast no\n" |
| 533 | " mcast_port 50\n" |
| 534 | " mcast_group hello\n" |
| 535 | " }\n" |
| 536 | "}\n"; |
| 537 | |
| 538 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 539 | bind(&isExpectedException, _1, |
| 540 | "Invalid value for option \"mcast_group\" in \"udp\" section")); |
| 541 | } |
| 542 | |
| 543 | BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcastGroupV6) |
| 544 | { |
| 545 | const std::string CONFIG = |
| 546 | "face_system\n" |
| 547 | "{\n" |
| 548 | " udp\n" |
| 549 | " {\n" |
| 550 | " mcast no\n" |
| 551 | " mcast_port 50\n" |
| 552 | " mcast_group ::1\n" |
| 553 | " }\n" |
| 554 | "}\n"; |
| 555 | |
| 556 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 557 | bind(&isExpectedException, _1, |
| 558 | "Invalid value for option \"mcast_group\" in \"udp\" section")); |
| 559 | } |
| 560 | |
| 561 | BOOST_AUTO_TEST_CASE(TestProcessSectionUdpUnknownOption) |
| 562 | { |
| 563 | const std::string CONFIG = |
| 564 | "face_system\n" |
| 565 | "{\n" |
| 566 | " udp\n" |
| 567 | " {\n" |
| 568 | " hello\n" |
| 569 | " }\n" |
| 570 | "}\n"; |
| 571 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 572 | bind(&isExpectedException, _1, |
| 573 | "Unrecognized option \"hello\" in \"udp\" section")); |
| 574 | } |
| 575 | |
| 576 | BOOST_AUTO_TEST_CASE(TestProcessSectionEther) |
| 577 | { |
| 578 | const std::string CONFIG = |
| 579 | "face_system\n" |
| 580 | "{\n" |
| 581 | " ether\n" |
| 582 | " {\n" |
| 583 | " mcast yes\n" |
| 584 | " mcast_group 01:00:5E:00:17:AA\n" |
| 585 | " }\n" |
| 586 | "}\n"; |
| 587 | |
| 588 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false)); |
| 589 | } |
| 590 | |
| 591 | BOOST_AUTO_TEST_CASE(TestProcessSectionEtherDryRun) |
| 592 | { |
| 593 | const std::string CONFIG = |
| 594 | "face_system\n" |
| 595 | "{\n" |
| 596 | " ether\n" |
| 597 | " {\n" |
| 598 | " mcast yes\n" |
| 599 | " mcast_group 01:00:5E:00:17:AA\n" |
| 600 | " }\n" |
| 601 | "}\n"; |
| 602 | |
| 603 | BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true)); |
| 604 | } |
| 605 | |
| 606 | BOOST_AUTO_TEST_CASE(TestProcessSectionEtherBadMcast) |
| 607 | { |
| 608 | const std::string CONFIG = |
| 609 | "face_system\n" |
| 610 | "{\n" |
| 611 | " ether\n" |
| 612 | " {\n" |
| 613 | " mcast hello\n" |
| 614 | " }\n" |
| 615 | "}\n"; |
| 616 | |
| 617 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 618 | bind(&isExpectedException, _1, |
| 619 | "Invalid value for option \"mcast\" in \"ether\" section")); |
| 620 | } |
| 621 | |
| 622 | BOOST_AUTO_TEST_CASE(TestProcessSectionEtherBadMcastGroup) |
| 623 | { |
| 624 | const std::string CONFIG = |
| 625 | "face_system\n" |
| 626 | "{\n" |
| 627 | " ether\n" |
| 628 | " {\n" |
| 629 | " mcast yes\n" |
| 630 | " mcast_group\n" |
| 631 | " }\n" |
| 632 | "}\n"; |
| 633 | |
| 634 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 635 | bind(&isExpectedException, _1, |
| 636 | "Invalid value for option \"mcast_group\" in \"ether\" section")); |
| 637 | } |
| 638 | |
| 639 | BOOST_AUTO_TEST_CASE(TestProcessSectionEtherUnknownOption) |
| 640 | { |
| 641 | const std::string CONFIG = |
| 642 | "face_system\n" |
| 643 | "{\n" |
| 644 | " ether\n" |
| 645 | " {\n" |
| 646 | " hello\n" |
| 647 | " }\n" |
| 648 | "}\n"; |
| 649 | BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error, |
| 650 | bind(&isExpectedException, _1, |
| 651 | "Unrecognized option \"hello\" in \"ether\" section")); |
| 652 | } |
| 653 | |
| 654 | BOOST_AUTO_TEST_CASE(TestFireInterestFilter) |
| 655 | { |
| 656 | shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/faces")); |
| 657 | |
| 658 | getFace()->onReceiveData += |
| 659 | bind(&FaceManagerFixture::validateControlResponse, this, _1, |
| 660 | command->getName(), 400, "Malformed command"); |
| 661 | |
| 662 | getFace()->sendInterest(*command); |
| 663 | |
| 664 | BOOST_REQUIRE(didCallbackFire()); |
| 665 | } |
| 666 | |
| 667 | BOOST_AUTO_TEST_CASE(MalformedCommmand) |
| 668 | { |
| 669 | shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/faces")); |
| 670 | |
| 671 | getFace()->onReceiveData += |
| 672 | bind(&FaceManagerFixture::validateControlResponse, this, _1, |
| 673 | command->getName(), 400, "Malformed command"); |
| 674 | |
| 675 | getManager().onFaceRequest(*command); |
| 676 | |
| 677 | BOOST_REQUIRE(didCallbackFire()); |
| 678 | } |
| 679 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 680 | BOOST_AUTO_TEST_CASE(UnsignedCommand) |
| 681 | { |
| 682 | ndn::nfd::FaceManagementOptions options; |
| 683 | options.setUri("tcp://127.0.0.1"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 684 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 685 | Block encodedOptions(options.wireEncode()); |
| 686 | |
| 687 | Name commandName("/localhost/nfd/faces"); |
| 688 | commandName.append("create"); |
| 689 | commandName.append(encodedOptions); |
| 690 | |
| 691 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 692 | |
| 693 | getFace()->onReceiveData += |
| 694 | bind(&FaceManagerFixture::validateControlResponse, this, _1, |
| 695 | command->getName(), 401, "Signature required"); |
| 696 | |
| 697 | getManager().onFaceRequest(*command); |
| 698 | |
| 699 | BOOST_REQUIRE(didCallbackFire()); |
| 700 | } |
| 701 | |
| 702 | BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedCommandFixture<FaceManagerFixture>) |
| 703 | { |
| 704 | ndn::nfd::FaceManagementOptions options; |
| 705 | options.setUri("tcp://127.0.0.1"); |
| 706 | |
| 707 | Block encodedOptions(options.wireEncode()); |
| 708 | |
| 709 | Name commandName("/localhost/nfd/faces"); |
| 710 | commandName.append("create"); |
| 711 | commandName.append(encodedOptions); |
| 712 | |
| 713 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 714 | generateCommand(*command); |
| 715 | |
| 716 | getFace()->onReceiveData += |
| 717 | bind(&FaceManagerFixture::validateControlResponse, this, _1, |
| 718 | command->getName(), 403, "Unauthorized command"); |
| 719 | |
| 720 | getManager().onFaceRequest(*command); |
| 721 | |
| 722 | BOOST_REQUIRE(didCallbackFire()); |
| 723 | } |
| 724 | |
| 725 | template <typename T> class AuthorizedCommandFixture : public CommandFixture<T> |
| 726 | { |
| 727 | public: |
| 728 | AuthorizedCommandFixture() |
| 729 | { |
| 730 | const std::string regex = "^<localhost><nfd><faces>"; |
| 731 | T::addInterestRule(regex, *CommandFixture<T>::m_certificate); |
| 732 | } |
| 733 | |
| 734 | virtual |
| 735 | ~AuthorizedCommandFixture() |
| 736 | { |
| 737 | |
| 738 | } |
| 739 | }; |
| 740 | |
| 741 | // template <> class AuthorizedCommandFixture<FaceManagerFixture> : |
| 742 | // public CommandFixture<FaceManagerFixture> |
| 743 | // { |
| 744 | // public: |
| 745 | // AuthorizedCommandFixture() |
| 746 | // { |
| 747 | // const std::string regex = "^<localhost><nfd><faces>"; |
| 748 | // FaceManagerFixture::ManagerBase::addInterestRule(regex, *CommandFixture<FaceManagerFixture>::m_certificate); |
| 749 | // } |
| 750 | |
| 751 | // virtual |
| 752 | // ~AuthorizedCommandFixture() |
| 753 | // { |
| 754 | |
| 755 | // } |
| 756 | // }; |
| 757 | |
| 758 | BOOST_FIXTURE_TEST_CASE(UnsupportedCommand, AuthorizedCommandFixture<FaceManagerFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 759 | { |
| 760 | ndn::nfd::FaceManagementOptions options; |
| 761 | |
| 762 | Block encodedOptions(options.wireEncode()); |
| 763 | |
| 764 | Name commandName("/localhost/nfd/faces"); |
| 765 | commandName.append("unsupported"); |
| 766 | commandName.append(encodedOptions); |
| 767 | |
| 768 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 769 | generateCommand(*command); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 770 | |
| 771 | getFace()->onReceiveData += |
| 772 | bind(&FaceManagerFixture::validateControlResponse, this, _1, |
| 773 | command->getName(), 501, "Unsupported command"); |
| 774 | |
| 775 | getManager().onFaceRequest(*command); |
| 776 | |
| 777 | BOOST_REQUIRE(didCallbackFire()); |
| 778 | } |
| 779 | |
| 780 | class ValidatedFaceRequestFixture : public TestFaceTableFixture, |
| 781 | public TestFaceManagerCommon, |
| 782 | public FaceManager |
| 783 | { |
| 784 | public: |
| 785 | |
| 786 | ValidatedFaceRequestFixture() |
| 787 | : FaceManager(TestFaceTableFixture::m_faceTable, TestFaceManagerCommon::m_face), |
| 788 | m_createFaceFired(false), |
| 789 | m_destroyFaceFired(false) |
| 790 | { |
| 791 | |
| 792 | } |
| 793 | |
| 794 | virtual void |
| 795 | createFace(const Name& requestName, |
| 796 | ndn::nfd::FaceManagementOptions& options) |
| 797 | { |
| 798 | m_createFaceFired = true; |
| 799 | } |
| 800 | |
| 801 | virtual void |
| 802 | destroyFace(const Name& requestName, |
| 803 | ndn::nfd::FaceManagementOptions& options) |
| 804 | { |
| 805 | m_destroyFaceFired = true; |
| 806 | } |
| 807 | |
| 808 | virtual |
| 809 | ~ValidatedFaceRequestFixture() |
| 810 | { |
| 811 | |
| 812 | } |
| 813 | |
| 814 | bool |
| 815 | didCreateFaceFire() const |
| 816 | { |
| 817 | return m_createFaceFired; |
| 818 | } |
| 819 | |
| 820 | bool |
| 821 | didDestroyFaceFire() const |
| 822 | { |
| 823 | return m_destroyFaceFired; |
| 824 | } |
| 825 | |
| 826 | private: |
| 827 | bool m_createFaceFired; |
| 828 | bool m_destroyFaceFired; |
| 829 | }; |
| 830 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 831 | BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestBadOptionParse, |
| 832 | AuthorizedCommandFixture<ValidatedFaceRequestFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 833 | { |
| 834 | Name commandName("/localhost/nfd/faces"); |
| 835 | commandName.append("create"); |
| 836 | commandName.append("NotReallyOptions"); |
| 837 | |
| 838 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 839 | generateCommand(*command); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 840 | |
| 841 | getFace()->onReceiveData += |
| 842 | bind(&ValidatedFaceRequestFixture::validateControlResponse, this, _1, |
| 843 | command->getName(), 400, "Malformed command"); |
| 844 | |
| 845 | onValidatedFaceRequest(command); |
| 846 | |
| 847 | BOOST_REQUIRE(didCallbackFire()); |
| 848 | } |
| 849 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 850 | BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestCreateFace, |
| 851 | AuthorizedCommandFixture<ValidatedFaceRequestFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 852 | { |
| 853 | ndn::nfd::FaceManagementOptions options; |
| 854 | options.setUri("tcp://127.0.0.1"); |
| 855 | |
| 856 | Block encodedOptions(options.wireEncode()); |
| 857 | |
| 858 | Name commandName("/localhost/nfd/faces"); |
| 859 | commandName.append("create"); |
| 860 | commandName.append(encodedOptions); |
| 861 | |
| 862 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 863 | generateCommand(*command); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 864 | |
| 865 | onValidatedFaceRequest(command); |
| 866 | BOOST_CHECK(didCreateFaceFire()); |
| 867 | } |
| 868 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 869 | BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestDestroyFace, |
| 870 | AuthorizedCommandFixture<ValidatedFaceRequestFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 871 | { |
| 872 | ndn::nfd::FaceManagementOptions options; |
| 873 | options.setUri("tcp://127.0.0.1"); |
| 874 | |
| 875 | Block encodedOptions(options.wireEncode()); |
| 876 | |
| 877 | Name commandName("/localhost/nfd/faces"); |
| 878 | commandName.append("destroy"); |
| 879 | commandName.append(encodedOptions); |
| 880 | |
| 881 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 882 | generateCommand(*command); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 883 | |
| 884 | onValidatedFaceRequest(command); |
| 885 | BOOST_CHECK(didDestroyFaceFire()); |
| 886 | } |
| 887 | |
| 888 | class FaceFixture : public TestFaceTableFixture, |
| 889 | public TestFaceManagerCommon, |
| 890 | public FaceManager |
| 891 | { |
| 892 | public: |
| 893 | FaceFixture() |
| 894 | : FaceManager(TestFaceTableFixture::m_faceTable, TestFaceManagerCommon::m_face) |
| 895 | { |
| 896 | |
| 897 | } |
| 898 | |
| 899 | virtual |
| 900 | ~FaceFixture() |
| 901 | { |
| 902 | |
| 903 | } |
| 904 | }; |
| 905 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 906 | BOOST_FIXTURE_TEST_CASE(CreateFaceBadUri, AuthorizedCommandFixture<FaceFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 907 | { |
| 908 | ndn::nfd::FaceManagementOptions options; |
| 909 | options.setUri("tcp:/127.0.0.1"); |
| 910 | |
| 911 | Block encodedOptions(options.wireEncode()); |
| 912 | |
| 913 | Name commandName("/localhost/nfd/faces"); |
| 914 | commandName.append("create"); |
| 915 | commandName.append(encodedOptions); |
| 916 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 917 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 918 | generateCommand(*command); |
| 919 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 920 | getFace()->onReceiveData += |
| 921 | bind(&FaceFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 922 | command->getName(), 400, "Malformed command"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 923 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 924 | createFace(command->getName(), options); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 925 | |
| 926 | BOOST_REQUIRE(didCallbackFire()); |
| 927 | } |
| 928 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 929 | BOOST_FIXTURE_TEST_CASE(CreateFaceUnknownScheme, AuthorizedCommandFixture<FaceFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 930 | { |
| 931 | ndn::nfd::FaceManagementOptions options; |
| 932 | // this will be an unsupported protocol because no factories have been |
| 933 | // added to the face manager |
| 934 | options.setUri("tcp://127.0.0.1"); |
| 935 | |
| 936 | Block encodedOptions(options.wireEncode()); |
| 937 | |
| 938 | Name commandName("/localhost/nfd/faces"); |
| 939 | commandName.append("create"); |
| 940 | commandName.append(encodedOptions); |
| 941 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 942 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 943 | generateCommand(*command); |
| 944 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 945 | getFace()->onReceiveData += |
| 946 | bind(&FaceFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 947 | command->getName(), 501, "Unsupported protocol"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 948 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 949 | createFace(command->getName(), options); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 950 | |
| 951 | BOOST_REQUIRE(didCallbackFire()); |
| 952 | } |
| 953 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 954 | BOOST_FIXTURE_TEST_CASE(OnCreated, AuthorizedCommandFixture<FaceFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 955 | { |
| 956 | ndn::nfd::FaceManagementOptions options; |
| 957 | options.setUri("tcp://127.0.0.1"); |
| 958 | |
| 959 | Block encodedOptions(options.wireEncode()); |
| 960 | |
| 961 | Name commandName("/localhost/nfd/faces"); |
| 962 | commandName.append("create"); |
| 963 | commandName.append(encodedOptions); |
| 964 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 965 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 966 | generateCommand(*command); |
| 967 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 968 | ndn::nfd::FaceManagementOptions resultOptions; |
| 969 | resultOptions.setUri("tcp://127.0.0.1"); |
| 970 | resultOptions.setFaceId(-1); |
| 971 | |
| 972 | Block encodedResultOptions(resultOptions.wireEncode()); |
| 973 | |
| 974 | getFace()->onReceiveData += |
| 975 | bind(&FaceFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 976 | command->getName(), 200, "Success", encodedResultOptions); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 977 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 978 | onCreated(command->getName(), options, make_shared<DummyFace>()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 979 | |
| 980 | BOOST_REQUIRE(didCallbackFire()); |
| 981 | BOOST_CHECK(TestFaceTableFixture::m_faceTable.didAddFire()); |
| 982 | } |
| 983 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 984 | BOOST_FIXTURE_TEST_CASE(OnConnectFailed, AuthorizedCommandFixture<FaceFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 985 | { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 986 | ndn::nfd::FaceManagementOptions options; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 987 | options.setUri("tcp://127.0.0.1"); |
| 988 | |
| 989 | Block encodedOptions(options.wireEncode()); |
| 990 | |
| 991 | Name commandName("/localhost/nfd/faces"); |
| 992 | commandName.append("create"); |
| 993 | commandName.append(encodedOptions); |
| 994 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 995 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 996 | generateCommand(*command); |
| 997 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 998 | getFace()->onReceiveData += |
| 999 | bind(&FaceFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 1000 | command->getName(), 400, "Failed to create face"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1001 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 1002 | onConnectFailed(command->getName(), "unit-test-reason"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1003 | |
| 1004 | BOOST_REQUIRE(didCallbackFire()); |
| 1005 | BOOST_CHECK_EQUAL(TestFaceTableFixture::m_faceTable.didAddFire(), false); |
| 1006 | } |
| 1007 | |
| 1008 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 1009 | BOOST_FIXTURE_TEST_CASE(DestroyFace, AuthorizedCommandFixture<FaceFixture>) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1010 | { |
| 1011 | ndn::nfd::FaceManagementOptions options; |
| 1012 | options.setUri("tcp://127.0.0.1"); |
| 1013 | |
| 1014 | Block encodedOptions(options.wireEncode()); |
| 1015 | |
| 1016 | Name commandName("/localhost/nfd/faces"); |
| 1017 | commandName.append("destroy"); |
| 1018 | commandName.append(encodedOptions); |
| 1019 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 1020 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 1021 | generateCommand(*command); |
| 1022 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1023 | getFace()->onReceiveData += |
| 1024 | bind(&FaceFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 1025 | command->getName(), 200, "Success"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1026 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 1027 | destroyFace(command->getName(), options); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1028 | |
| 1029 | BOOST_REQUIRE(didCallbackFire()); |
| 1030 | BOOST_CHECK(TestFaceTableFixture::m_faceTable.didRemoveFire()); |
| 1031 | BOOST_CHECK(TestFaceTableFixture::m_faceTable.getDummyFace()->didCloseFire()); |
| 1032 | } |
| 1033 | |
| 1034 | BOOST_AUTO_TEST_SUITE_END() |
| 1035 | |
| 1036 | } // namespace tests |
| 1037 | } // namespace nfd |
| 1038 | |