blob: 00a94513f0d5c59a0e9b94e1f7b345ac0393048c [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"
Junxiao Shi84d62cb2017-07-12 16:15:18 +000033#include "test-netif-ip.hpp"
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070034#include "tests/limited-io.hpp"
35
36namespace nfd {
37namespace face {
38namespace tests {
39
40using namespace nfd::tests;
41namespace ip = boost::asio::ip;
42using ip::udp;
43
44class MulticastUdpTransportFixture : public BaseFixture
45{
46protected:
47 MulticastUdpTransportFixture()
48 : transport(nullptr)
49 , multicastEp(ip::address::from_string("230.15.19.47"), 7070)
Junxiao Shi84d62cb2017-07-12 16:15:18 +000050 , defaultAddr(getTestIp<AddressFamily::V4>(LoopbackAddress::No, MulticastInterface::Yes))
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070051 , receivedPackets(nullptr)
52 , remoteSockRx(g_io)
53 , remoteSockTx(g_io)
54 {
55 }
56
57 void
58 initialize(ip::address_v4 address)
59 {
60 openMulticastSockets(remoteSockRx, remoteSockTx, multicastEp.port());
61
62 udp::socket sockRx(g_io);
63 udp::socket sockTx(g_io);
64 localEp = udp::endpoint(address, 7001);
65 openMulticastSockets(sockRx, sockTx, localEp.port());
Teng Liangfe4fce32017-03-29 04:49:38 +000066 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070067
68 face = make_unique<Face>(
69 make_unique<DummyReceiveLinkService>(),
Teng Liangfe4fce32017-03-29 04:49:38 +000070 make_unique<MulticastUdpTransport>(localEp, multicastEp, std::move(sockRx),
71 std::move(sockTx), linkType));
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070072 transport = static_cast<MulticastUdpTransport*>(face->getTransport());
73 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
74
75 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
76 }
77
78 void
79 openMulticastSockets(udp::socket& rx, udp::socket& tx, uint16_t port)
80 {
81 rx.open(udp::v4());
82 rx.set_option(udp::socket::reuse_address(true));
83 rx.bind(udp::endpoint(multicastEp.address(), port));
84 rx.set_option(ip::multicast::join_group(multicastEp.address()));
85
86 tx.open(udp::v4());
87 tx.set_option(udp::socket::reuse_address(true));
88 tx.set_option(ip::multicast::enable_loopback(true));
89 tx.bind(udp::endpoint(ip::address_v4::any(), port));
90 }
91
92 void
93 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
94 {
95 remoteSockRx.async_receive(boost::asio::buffer(buf),
96 [this, needToCheck] (const boost::system::error_code& error, size_t) {
97 if (needToCheck) {
98 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
99 }
100 limitedIo.afterOp();
101 });
102 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
103 }
104
105 void
106 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
107 {
108 remoteSockTx.async_send_to(boost::asio::buffer(buf), udp::endpoint(multicastEp.address(), 7001),
109 [needToCheck] (const boost::system::error_code& error, size_t) {
110 if (needToCheck) {
111 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
112 }
113 });
114 limitedIo.defer(time::seconds(1));
115 }
116
117protected:
118 LimitedIo limitedIo;
119 MulticastUdpTransport* transport;
120 udp::endpoint localEp;
121 udp::endpoint multicastEp;
122 const ip::address_v4 defaultAddr;
123 std::vector<Transport::Packet>* receivedPackets;
124
125private:
126 unique_ptr<Face> face;
127 udp::socket remoteSockRx;
128 udp::socket remoteSockTx;
129};
130
131} // namespace tests
132} // namespace face
133} // namespace nfd
134
135#endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP