blob: 775b12ddc26b057b7a1fe69202d52022d46687cc [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 Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, 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 Pesaventocf7db2f2019-03-24 23:17:28 -040034#include "dummy-receive-link-service.hpp"
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070035
36namespace nfd {
37namespace face {
38namespace tests {
39
40using namespace nfd::tests;
41namespace ip = boost::asio::ip;
42using ip::udp;
43
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040044class MulticastUdpTransportFixture : public GlobalIoFixture
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070045{
46protected:
47 MulticastUdpTransportFixture()
48 : transport(nullptr)
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050049 , txPort(7001)
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070050 , receivedPackets(nullptr)
51 , remoteSockRx(g_io)
52 , remoteSockTx(g_io)
53 {
54 }
55
56 void
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040057 initialize(ip::address address)
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070058 {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050059 ip::address mcastAddr;
60 if (address.is_v4()) {
61 // the administratively scoped group 224.0.0.254 is reserved for experimentation (RFC 4727)
62 mcastAddr = ip::address_v4(0xE00000FE);
63 }
64 else {
65 // the group FF0X::114 is reserved for experimentation at all scope levels (RFC 4727)
66 auto v6Addr = ip::address_v6::from_string("FF01::114");
67 v6Addr.scope_id(address.to_v6().scope_id());
68 mcastAddr = v6Addr;
69 }
70 mcastEp = udp::endpoint(mcastAddr, 7373);
71 remoteMcastEp = udp::endpoint(mcastAddr, 8383);
Davide Pesavento8215a3a2017-12-25 19:14:33 -050072
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050073 MulticastUdpTransport::openRxSocket(remoteSockRx, mcastEp, address);
74 MulticastUdpTransport::openTxSocket(remoteSockTx, udp::endpoint(address, 0), nullptr, true);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070075
76 udp::socket sockRx(g_io);
77 udp::socket sockTx(g_io);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050078 MulticastUdpTransport::openRxSocket(sockRx, remoteMcastEp, address);
79 MulticastUdpTransport::openTxSocket(sockTx, udp::endpoint(address, txPort), nullptr, true);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070080
81 face = make_unique<Face>(
82 make_unique<DummyReceiveLinkService>(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050083 make_unique<MulticastUdpTransport>(mcastEp, std::move(sockRx), std::move(sockTx),
84 ndn::nfd::LINK_TYPE_MULTI_ACCESS));
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070085 transport = static_cast<MulticastUdpTransport*>(face->getTransport());
86 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
87
88 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
89 }
90
91 void
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070092 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
93 {
94 remoteSockRx.async_receive(boost::asio::buffer(buf),
95 [this, needToCheck] (const boost::system::error_code& error, size_t) {
96 if (needToCheck) {
97 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
98 }
99 limitedIo.afterOp();
100 });
101 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
102 }
103
104 void
105 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
106 {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500107 sendToGroup(remoteSockTx, buf, needToCheck);
108 limitedIo.defer(time::seconds(1));
109 }
110
111 void
112 sendToGroup(udp::socket& sock, const std::vector<uint8_t>& buf, bool needToCheck = true) const
113 {
114 sock.async_send_to(boost::asio::buffer(buf), remoteMcastEp,
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700115 [needToCheck] (const boost::system::error_code& error, size_t) {
116 if (needToCheck) {
117 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
118 }
119 });
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700120 }
121
122protected:
123 LimitedIo limitedIo;
124 MulticastUdpTransport* transport;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500125 udp::endpoint mcastEp;
126 uint16_t txPort;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700127 std::vector<Transport::Packet>* receivedPackets;
128
129private:
130 unique_ptr<Face> face;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500131 udp::endpoint remoteMcastEp;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700132 udp::socket remoteSockRx;
133 udp::socket remoteSockTx;
134};
135
136} // namespace tests
137} // namespace face
138} // namespace nfd
139
140#endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP