blob: 4c984f45d91ec440be29649aaa89b9f347aaed80 [file] [log] [blame]
Eric Newberry6d8ee7a2015-12-21 16:37:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Eric Newberry6d8ee7a2015-12-21 16:37:52 -07004 * 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#ifndef NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP
28
29#include "face/multicast-udp-transport.hpp"
30#include "face/face.hpp"
31
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040032#include "tests/test-common.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060033#include "tests/daemon/limited-io.hpp"
Davide Pesaventoa8098582019-03-31 15:48:02 -040034#include "tests/daemon/face/dummy-link-service.hpp"
Davide Pesavento279af1c2022-08-29 20:18:32 -040035#include "tests/daemon/face/transport-test-common.hpp"
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::tests {
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070038
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070039namespace ip = boost::asio::ip;
40using ip::udp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040041using face::MulticastUdpTransport;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070042
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040043class MulticastUdpTransportFixture : public GlobalIoFixture
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070044{
45protected:
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070046 void
Davide Pesavento279af1c2022-08-29 20:18:32 -040047 initialize(const shared_ptr<const ndn::net::NetworkInterface>& netif, const ip::address& address)
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070048 {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050049 ip::address mcastAddr;
50 if (address.is_v4()) {
51 // the administratively scoped group 224.0.0.254 is reserved for experimentation (RFC 4727)
52 mcastAddr = ip::address_v4(0xE00000FE);
53 }
54 else {
55 // the group FF0X::114 is reserved for experimentation at all scope levels (RFC 4727)
56 auto v6Addr = ip::address_v6::from_string("FF01::114");
Davide Pesavento279af1c2022-08-29 20:18:32 -040057 v6Addr.scope_id(netif->getIndex());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050058 mcastAddr = v6Addr;
59 }
60 mcastEp = udp::endpoint(mcastAddr, 7373);
61 remoteMcastEp = udp::endpoint(mcastAddr, 8383);
Davide Pesavento8215a3a2017-12-25 19:14:33 -050062
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050063 MulticastUdpTransport::openRxSocket(remoteSockRx, mcastEp, address);
Davide Pesavento279af1c2022-08-29 20:18:32 -040064 MulticastUdpTransport::openTxSocket(remoteSockTx, udp::endpoint(address, 0), &*netif, true);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070065
66 udp::socket sockRx(g_io);
67 udp::socket sockTx(g_io);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050068 MulticastUdpTransport::openRxSocket(sockRx, remoteMcastEp, address);
Davide Pesavento279af1c2022-08-29 20:18:32 -040069 MulticastUdpTransport::openTxSocket(sockTx, udp::endpoint(address, txPort), &*netif, true);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070070
Davide Pesaventoa8098582019-03-31 15:48:02 -040071 face = make_unique<Face>(make_unique<DummyLinkService>(),
72 make_unique<MulticastUdpTransport>(mcastEp, std::move(sockRx), std::move(sockTx),
73 ndn::nfd::LINK_TYPE_MULTI_ACCESS));
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070074 transport = static_cast<MulticastUdpTransport*>(face->getTransport());
Davide Pesaventoa8098582019-03-31 15:48:02 -040075 receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070076
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040077 BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070078 }
79
80 void
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070081 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
82 {
83 remoteSockRx.async_receive(boost::asio::buffer(buf),
84 [this, needToCheck] (const boost::system::error_code& error, size_t) {
85 if (needToCheck) {
86 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
87 }
88 limitedIo.afterOp();
89 });
Davide Pesavento14e71f02019-03-28 17:35:25 -040090 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070091 }
92
93 void
94 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
95 {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050096 sendToGroup(remoteSockTx, buf, needToCheck);
Davide Pesavento14e71f02019-03-28 17:35:25 -040097 limitedIo.defer(1_s);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050098 }
99
100 void
101 sendToGroup(udp::socket& sock, const std::vector<uint8_t>& buf, bool needToCheck = true) const
102 {
103 sock.async_send_to(boost::asio::buffer(buf), remoteMcastEp,
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400104 [needToCheck] (const auto& error, size_t) {
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700105 if (needToCheck) {
106 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
107 }
108 });
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700109 }
110
111protected:
112 LimitedIo limitedIo;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400113 MulticastUdpTransport* transport = nullptr;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500114 udp::endpoint mcastEp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400115 uint16_t txPort = 7001;
116 std::vector<RxPacket>* receivedPackets = nullptr;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700117
118private:
119 unique_ptr<Face> face;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500120 udp::endpoint remoteMcastEp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400121 udp::socket remoteSockRx{g_io};
122 udp::socket remoteSockTx{g_io};
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700123};
124
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400125} // namespace nfd::tests
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700126
127#endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP