blob: eaaca51cf4630132bf240a81a0eb63d02456b7f3 [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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace ndn::autoconfig::tests {
Junxiao Shia5a3a872017-07-08 12:42:11 +000034
35BOOST_AUTO_TEST_SUITE(NdnAutoconfig)
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036BOOST_FIXTURE_TEST_SUITE(TestMulticastDiscovery, ::nfd::tests::MockNfdMgmtFixture)
Junxiao Shia5a3a872017-07-08 12:42:11 +000037
38BOOST_AUTO_TEST_CASE(Normal)
39{
40 this->processInterest = [this] (const Interest& interest) {
41 if (Name("/localhost/nfd/faces/query").isPrefixOf(interest.getName())) {
42 nfd::FaceStatus payload1, payload2;
43 payload1.setFaceId(860)
44 .setRemoteUri("ether://[ff:ff:ff:ff:ff:ff]")
45 .setLocalUri("dev://eth0")
46 .setFaceScope(nfd::FACE_SCOPE_NON_LOCAL)
47 .setFacePersistency(nfd::FACE_PERSISTENCY_PERMANENT)
48 .setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
49 payload2.setFaceId(861)
50 .setRemoteUri("ether://[ff:ff:ff:ff:ff:ff]")
51 .setLocalUri("dev://eth1")
52 .setFaceScope(nfd::FACE_SCOPE_NON_LOCAL)
53 .setFacePersistency(nfd::FACE_PERSISTENCY_PERMANENT)
54 .setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
55 this->sendDataset(interest.getName(), payload1, payload2);
56 return;
57 }
58
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040059 auto req = parseCommand(interest, "/localhost/nfd/rib/register");
Junxiao Shia5a3a872017-07-08 12:42:11 +000060 if (req) {
61 BOOST_REQUIRE(req->hasName());
62 BOOST_CHECK_EQUAL(req->getName(), "/localhop/ndn-autoconf/hub");
63 BOOST_REQUIRE(req->hasFaceId());
64
65 if (req->getFaceId() == 860) {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040066 nfd::ControlParameters resp;
Junxiao Shia5a3a872017-07-08 12:42:11 +000067 resp.setName("/localhop/ndn-autoconf/hub")
68 .setFaceId(860)
69 .setOrigin(nfd::ROUTE_ORIGIN_APP)
70 .setCost(1)
71 .setFlags(0);
72 this->succeedCommand(interest, resp);
73 }
74 else if (req->getFaceId() == 861) {
75 // no reply, but stage should continue when there is at least one successful registration
76 }
77 else {
78 BOOST_ERROR("unexpected rib/register command");
79 }
80 return;
81 }
82
83 req = parseCommand(interest, "/localhost/nfd/strategy-choice/set");
84 if (req) {
85 BOOST_REQUIRE(req->hasName());
86 BOOST_CHECK_EQUAL(req->getName(), "/localhop/ndn-autoconf/hub");
87 BOOST_REQUIRE(req->hasStrategy());
88 BOOST_CHECK_EQUAL(req->getStrategy(), "/localhost/nfd/strategy/multicast");
89
90 this->succeedCommand(interest, *req);
91 return;
92 }
93
94 if (interest.getName() == "/localhop/ndn-autoconf/hub") {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040095 auto data = ::nfd::tests::makeData(Name("/localhop/ndn-autoconf/hub").appendVersion());
Junxiao Shi73b49802019-05-25 07:52:26 +000096 data->setFreshnessPeriod(1_s);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -050097 data->setContent(makeStringBlock(tlv::nfd::Uri, "udp://router.example.net"));
Junxiao Shia5a3a872017-07-08 12:42:11 +000098 face.receive(*data);
99 return;
100 }
101
102 BOOST_ERROR("unexpected Interest " << interest);
103 };
104
105 nfd::Controller controller(face, m_keyChain);
106 MulticastDiscovery stage(face, controller);
107
108 bool hasSuccess = false;
109 stage.onSuccess.connect([&hasSuccess] (const FaceUri& u) {
110 BOOST_CHECK(!hasSuccess);
111 hasSuccess = true;
112
113 BOOST_CHECK(u == FaceUri("udp://router.example.net"));
114 });
115
116 stage.onFailure.connect([] (const std::string& reason) {
117 BOOST_ERROR("unexpected failure: " << reason);
118 });
119
120 stage.start();
Davide Pesavento14e71f02019-03-28 17:35:25 -0400121 face.processEvents(20_s);
Junxiao Shia5a3a872017-07-08 12:42:11 +0000122 BOOST_CHECK(hasSuccess);
123}
124
125BOOST_AUTO_TEST_SUITE_END() // TestMulticastDiscovery
126BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig
127
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400128} // namespace ndn::autoconfig::tests