blob: 9a0f5ac43cb60ff616b32a3046fcac2156bf5237 [file] [log] [blame]
Junxiao Shia5a3a872017-07-08 12:42:11 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shia5a3a872017-07-08 12:42:11 +00004 * 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
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040028#include "tests/tools/mock-nfd-mgmt-fixture.hpp"
29
Junxiao Shia5a3a872017-07-08 12:42:11 +000030#include <ndn-cxx/encoding/block-helpers.hpp>
31#include <ndn-cxx/encoding/tlv-nfd.hpp>
32
33namespace ndn {
34namespace tools {
35namespace autoconfig {
36namespace tests {
37
38using namespace ::nfd::tools::tests;
39using nfd::ControlParameters;
40
41BOOST_AUTO_TEST_SUITE(NdnAutoconfig)
42BOOST_FIXTURE_TEST_SUITE(TestMulticastDiscovery, MockNfdMgmtFixture)
43
44BOOST_AUTO_TEST_CASE(Normal)
45{
46 this->processInterest = [this] (const Interest& interest) {
47 if (Name("/localhost/nfd/faces/query").isPrefixOf(interest.getName())) {
48 nfd::FaceStatus payload1, payload2;
49 payload1.setFaceId(860)
50 .setRemoteUri("ether://[ff:ff:ff:ff:ff:ff]")
51 .setLocalUri("dev://eth0")
52 .setFaceScope(nfd::FACE_SCOPE_NON_LOCAL)
53 .setFacePersistency(nfd::FACE_PERSISTENCY_PERMANENT)
54 .setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
55 payload2.setFaceId(861)
56 .setRemoteUri("ether://[ff:ff:ff:ff:ff:ff]")
57 .setLocalUri("dev://eth1")
58 .setFaceScope(nfd::FACE_SCOPE_NON_LOCAL)
59 .setFacePersistency(nfd::FACE_PERSISTENCY_PERMANENT)
60 .setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
61 this->sendDataset(interest.getName(), payload1, payload2);
62 return;
63 }
64
65 optional<ControlParameters> req = parseCommand(interest, "/localhost/nfd/rib/register");
66 if (req) {
67 BOOST_REQUIRE(req->hasName());
68 BOOST_CHECK_EQUAL(req->getName(), "/localhop/ndn-autoconf/hub");
69 BOOST_REQUIRE(req->hasFaceId());
70
71 if (req->getFaceId() == 860) {
72 ControlParameters resp;
73 resp.setName("/localhop/ndn-autoconf/hub")
74 .setFaceId(860)
75 .setOrigin(nfd::ROUTE_ORIGIN_APP)
76 .setCost(1)
77 .setFlags(0);
78 this->succeedCommand(interest, resp);
79 }
80 else if (req->getFaceId() == 861) {
81 // no reply, but stage should continue when there is at least one successful registration
82 }
83 else {
84 BOOST_ERROR("unexpected rib/register command");
85 }
86 return;
87 }
88
89 req = parseCommand(interest, "/localhost/nfd/strategy-choice/set");
90 if (req) {
91 BOOST_REQUIRE(req->hasName());
92 BOOST_CHECK_EQUAL(req->getName(), "/localhop/ndn-autoconf/hub");
93 BOOST_REQUIRE(req->hasStrategy());
94 BOOST_CHECK_EQUAL(req->getStrategy(), "/localhost/nfd/strategy/multicast");
95
96 this->succeedCommand(interest, *req);
97 return;
98 }
99
100 if (interest.getName() == "/localhop/ndn-autoconf/hub") {
Junxiao Shia5a3a872017-07-08 12:42:11 +0000101 auto data = makeData(Name("/localhop/ndn-autoconf/hub").appendVersion());
Junxiao Shi73b49802019-05-25 07:52:26 +0000102 data->setFreshnessPeriod(1_s);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500103 data->setContent(makeStringBlock(tlv::nfd::Uri, "udp://router.example.net"));
Junxiao Shia5a3a872017-07-08 12:42:11 +0000104 face.receive(*data);
105 return;
106 }
107
108 BOOST_ERROR("unexpected Interest " << interest);
109 };
110
111 nfd::Controller controller(face, m_keyChain);
112 MulticastDiscovery stage(face, controller);
113
114 bool hasSuccess = false;
115 stage.onSuccess.connect([&hasSuccess] (const FaceUri& u) {
116 BOOST_CHECK(!hasSuccess);
117 hasSuccess = true;
118
119 BOOST_CHECK(u == FaceUri("udp://router.example.net"));
120 });
121
122 stage.onFailure.connect([] (const std::string& reason) {
123 BOOST_ERROR("unexpected failure: " << reason);
124 });
125
126 stage.start();
Davide Pesavento14e71f02019-03-28 17:35:25 -0400127 face.processEvents(20_s);
Junxiao Shia5a3a872017-07-08 12:42:11 +0000128 BOOST_CHECK(hasSuccess);
129}
130
131BOOST_AUTO_TEST_SUITE_END() // TestMulticastDiscovery
132BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig
133
134} // namespace tests
135} // namespace autoconfig
136} // namespace tools
137} // namespace ndn