Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "ndn-autoconfig/multicast-discovery.hpp" |
| 27 | |
| 28 | #include "../mock-nfd-mgmt-fixture.hpp" |
| 29 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 30 | #include <ndn-cxx/encoding/tlv-nfd.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace tools { |
| 34 | namespace autoconfig { |
| 35 | namespace tests { |
| 36 | |
| 37 | using namespace ::nfd::tools::tests; |
| 38 | using nfd::ControlParameters; |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(NdnAutoconfig) |
| 41 | BOOST_FIXTURE_TEST_SUITE(TestMulticastDiscovery, MockNfdMgmtFixture) |
| 42 | |
| 43 | BOOST_AUTO_TEST_CASE(Normal) |
| 44 | { |
| 45 | this->processInterest = [this] (const Interest& interest) { |
| 46 | if (Name("/localhost/nfd/faces/query").isPrefixOf(interest.getName())) { |
| 47 | nfd::FaceStatus payload1, payload2; |
| 48 | payload1.setFaceId(860) |
| 49 | .setRemoteUri("ether://[ff:ff:ff:ff:ff:ff]") |
| 50 | .setLocalUri("dev://eth0") |
| 51 | .setFaceScope(nfd::FACE_SCOPE_NON_LOCAL) |
| 52 | .setFacePersistency(nfd::FACE_PERSISTENCY_PERMANENT) |
| 53 | .setLinkType(nfd::LINK_TYPE_MULTI_ACCESS); |
| 54 | payload2.setFaceId(861) |
| 55 | .setRemoteUri("ether://[ff:ff:ff:ff:ff:ff]") |
| 56 | .setLocalUri("dev://eth1") |
| 57 | .setFaceScope(nfd::FACE_SCOPE_NON_LOCAL) |
| 58 | .setFacePersistency(nfd::FACE_PERSISTENCY_PERMANENT) |
| 59 | .setLinkType(nfd::LINK_TYPE_MULTI_ACCESS); |
| 60 | this->sendDataset(interest.getName(), payload1, payload2); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | optional<ControlParameters> req = parseCommand(interest, "/localhost/nfd/rib/register"); |
| 65 | if (req) { |
| 66 | BOOST_REQUIRE(req->hasName()); |
| 67 | BOOST_CHECK_EQUAL(req->getName(), "/localhop/ndn-autoconf/hub"); |
| 68 | BOOST_REQUIRE(req->hasFaceId()); |
| 69 | |
| 70 | if (req->getFaceId() == 860) { |
| 71 | ControlParameters resp; |
| 72 | resp.setName("/localhop/ndn-autoconf/hub") |
| 73 | .setFaceId(860) |
| 74 | .setOrigin(nfd::ROUTE_ORIGIN_APP) |
| 75 | .setCost(1) |
| 76 | .setFlags(0); |
| 77 | this->succeedCommand(interest, resp); |
| 78 | } |
| 79 | else if (req->getFaceId() == 861) { |
| 80 | // no reply, but stage should continue when there is at least one successful registration |
| 81 | } |
| 82 | else { |
| 83 | BOOST_ERROR("unexpected rib/register command"); |
| 84 | } |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | req = parseCommand(interest, "/localhost/nfd/strategy-choice/set"); |
| 89 | if (req) { |
| 90 | BOOST_REQUIRE(req->hasName()); |
| 91 | BOOST_CHECK_EQUAL(req->getName(), "/localhop/ndn-autoconf/hub"); |
| 92 | BOOST_REQUIRE(req->hasStrategy()); |
| 93 | BOOST_CHECK_EQUAL(req->getStrategy(), "/localhost/nfd/strategy/multicast"); |
| 94 | |
| 95 | this->succeedCommand(interest, *req); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (interest.getName() == "/localhop/ndn-autoconf/hub") { |
| 100 | const char FACEURI[] = "udp://router.example.net"; |
| 101 | auto data = makeData(Name("/localhop/ndn-autoconf/hub").appendVersion()); |
| 102 | data->setContent(makeBinaryBlock(tlv::nfd::Uri, FACEURI, ::strlen(FACEURI))); |
| 103 | face.receive(*data); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | BOOST_ERROR("unexpected Interest " << interest); |
| 108 | }; |
| 109 | |
| 110 | nfd::Controller controller(face, m_keyChain); |
| 111 | MulticastDiscovery stage(face, controller); |
| 112 | |
| 113 | bool hasSuccess = false; |
| 114 | stage.onSuccess.connect([&hasSuccess] (const FaceUri& u) { |
| 115 | BOOST_CHECK(!hasSuccess); |
| 116 | hasSuccess = true; |
| 117 | |
| 118 | BOOST_CHECK(u == FaceUri("udp://router.example.net")); |
| 119 | }); |
| 120 | |
| 121 | stage.onFailure.connect([] (const std::string& reason) { |
| 122 | BOOST_ERROR("unexpected failure: " << reason); |
| 123 | }); |
| 124 | |
| 125 | stage.start(); |
| 126 | face.processEvents(time::seconds(20)); |
| 127 | BOOST_CHECK(hasSuccess); |
| 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_SUITE_END() // TestMulticastDiscovery |
| 131 | BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig |
| 132 | |
| 133 | } // namespace tests |
| 134 | } // namespace autoconfig |
| 135 | } // namespace tools |
| 136 | } // namespace ndn |