blob: 7c3ca2115faa6550c3d111042f111277f9aaee06 [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/*
Teng Liangfe4fce32017-03-29 04:49:38 +00003 * Copyright (c) 2014-2017, 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
32#include "dummy-receive-link-service.hpp"
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070033#include "tests/limited-io.hpp"
34
35namespace nfd {
36namespace face {
37namespace tests {
38
39using namespace nfd::tests;
40namespace ip = boost::asio::ip;
41using ip::udp;
42
43class MulticastUdpTransportFixture : public BaseFixture
44{
45protected:
46 MulticastUdpTransportFixture()
47 : transport(nullptr)
48 , multicastEp(ip::address::from_string("230.15.19.47"), 7070)
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070049 , receivedPackets(nullptr)
50 , remoteSockRx(g_io)
51 , remoteSockTx(g_io)
52 {
53 }
54
55 void
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040056 initialize(ip::address address)
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070057 {
58 openMulticastSockets(remoteSockRx, remoteSockTx, multicastEp.port());
59
60 udp::socket sockRx(g_io);
61 udp::socket sockTx(g_io);
62 localEp = udp::endpoint(address, 7001);
63 openMulticastSockets(sockRx, sockTx, localEp.port());
64
65 face = make_unique<Face>(
66 make_unique<DummyReceiveLinkService>(),
Teng Liangfe4fce32017-03-29 04:49:38 +000067 make_unique<MulticastUdpTransport>(localEp, multicastEp, std::move(sockRx),
Davide Pesavento22fba352017-10-17 15:53:51 -040068 std::move(sockTx), ndn::nfd::LINK_TYPE_MULTI_ACCESS));
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070069 transport = static_cast<MulticastUdpTransport*>(face->getTransport());
70 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
71
72 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
73 }
74
75 void
76 openMulticastSockets(udp::socket& rx, udp::socket& tx, uint16_t port)
77 {
78 rx.open(udp::v4());
79 rx.set_option(udp::socket::reuse_address(true));
80 rx.bind(udp::endpoint(multicastEp.address(), port));
81 rx.set_option(ip::multicast::join_group(multicastEp.address()));
82
83 tx.open(udp::v4());
84 tx.set_option(udp::socket::reuse_address(true));
85 tx.set_option(ip::multicast::enable_loopback(true));
86 tx.bind(udp::endpoint(ip::address_v4::any(), port));
87 }
88
89 void
90 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
91 {
92 remoteSockRx.async_receive(boost::asio::buffer(buf),
93 [this, needToCheck] (const boost::system::error_code& error, size_t) {
94 if (needToCheck) {
95 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
96 }
97 limitedIo.afterOp();
98 });
99 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
100 }
101
102 void
103 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
104 {
105 remoteSockTx.async_send_to(boost::asio::buffer(buf), udp::endpoint(multicastEp.address(), 7001),
106 [needToCheck] (const boost::system::error_code& error, size_t) {
107 if (needToCheck) {
108 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
109 }
110 });
111 limitedIo.defer(time::seconds(1));
112 }
113
114protected:
115 LimitedIo limitedIo;
116 MulticastUdpTransport* transport;
117 udp::endpoint localEp;
118 udp::endpoint multicastEp;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700119 std::vector<Transport::Packet>* receivedPackets;
120
121private:
122 unique_ptr<Face> face;
123 udp::socket remoteSockRx;
124 udp::socket remoteSockTx;
125};
126
127} // namespace tests
128} // namespace face
129} // namespace nfd
130
131#endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP