blob: 959ca9c1a11bd2bf7af638491491ca99e857e1d0 [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"
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070037
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070038namespace ip = boost::asio::ip;
39using ip::udp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040using face::MulticastUdpTransport;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070041
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040042class MulticastUdpTransportFixture : public GlobalIoFixture
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070043{
44protected:
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070045 void
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040046 initialize(ip::address address)
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070047 {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050048 ip::address mcastAddr;
49 if (address.is_v4()) {
50 // the administratively scoped group 224.0.0.254 is reserved for experimentation (RFC 4727)
51 mcastAddr = ip::address_v4(0xE00000FE);
52 }
53 else {
54 // the group FF0X::114 is reserved for experimentation at all scope levels (RFC 4727)
55 auto v6Addr = ip::address_v6::from_string("FF01::114");
56 v6Addr.scope_id(address.to_v6().scope_id());
57 mcastAddr = v6Addr;
58 }
59 mcastEp = udp::endpoint(mcastAddr, 7373);
60 remoteMcastEp = udp::endpoint(mcastAddr, 8383);
Davide Pesavento8215a3a2017-12-25 19:14:33 -050061
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050062 MulticastUdpTransport::openRxSocket(remoteSockRx, mcastEp, address);
63 MulticastUdpTransport::openTxSocket(remoteSockTx, udp::endpoint(address, 0), nullptr, true);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070064
65 udp::socket sockRx(g_io);
66 udp::socket sockTx(g_io);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050067 MulticastUdpTransport::openRxSocket(sockRx, remoteMcastEp, address);
68 MulticastUdpTransport::openTxSocket(sockTx, udp::endpoint(address, txPort), nullptr, true);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070069
Davide Pesaventoa8098582019-03-31 15:48:02 -040070 face = make_unique<Face>(make_unique<DummyLinkService>(),
71 make_unique<MulticastUdpTransport>(mcastEp, std::move(sockRx), std::move(sockTx),
72 ndn::nfd::LINK_TYPE_MULTI_ACCESS));
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070073 transport = static_cast<MulticastUdpTransport*>(face->getTransport());
Davide Pesaventoa8098582019-03-31 15:48:02 -040074 receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070075
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040076 BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070077 }
78
79 void
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070080 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
81 {
82 remoteSockRx.async_receive(boost::asio::buffer(buf),
83 [this, needToCheck] (const boost::system::error_code& error, size_t) {
84 if (needToCheck) {
85 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
86 }
87 limitedIo.afterOp();
88 });
Davide Pesavento14e71f02019-03-28 17:35:25 -040089 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070090 }
91
92 void
93 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
94 {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050095 sendToGroup(remoteSockTx, buf, needToCheck);
Davide Pesavento14e71f02019-03-28 17:35:25 -040096 limitedIo.defer(1_s);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050097 }
98
99 void
100 sendToGroup(udp::socket& sock, const std::vector<uint8_t>& buf, bool needToCheck = true) const
101 {
102 sock.async_send_to(boost::asio::buffer(buf), remoteMcastEp,
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400103 [needToCheck] (const auto& error, size_t) {
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700104 if (needToCheck) {
105 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
106 }
107 });
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700108 }
109
110protected:
111 LimitedIo limitedIo;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400112 MulticastUdpTransport* transport = nullptr;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500113 udp::endpoint mcastEp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400114 uint16_t txPort = 7001;
115 std::vector<RxPacket>* receivedPackets = nullptr;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700116
117private:
118 unique_ptr<Face> face;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500119 udp::endpoint remoteMcastEp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400120 udp::socket remoteSockRx{g_io};
121 udp::socket remoteSockTx{g_io};
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700122};
123
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400124} // namespace nfd::tests
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700125
126#endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP