blob: b8ed524ebefdde6b44e83884812397569505c98e [file] [log] [blame]
Eric Newberry6d8ee7a2015-12-21 16:37:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoeee53aa2016-04-11 17:20:21 +02003 * Copyright (c) 2014-2016, 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"
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020033#include "test-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)
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020050 , defaultAddr(getTestIp<ip::address_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());
66
67 face = make_unique<Face>(
68 make_unique<DummyReceiveLinkService>(),
69 make_unique<MulticastUdpTransport>(localEp, multicastEp, std::move(sockRx), std::move(sockTx)));
70 transport = static_cast<MulticastUdpTransport*>(face->getTransport());
71 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
72
73 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
74 }
75
76 void
77 openMulticastSockets(udp::socket& rx, udp::socket& tx, uint16_t port)
78 {
79 rx.open(udp::v4());
80 rx.set_option(udp::socket::reuse_address(true));
81 rx.bind(udp::endpoint(multicastEp.address(), port));
82 rx.set_option(ip::multicast::join_group(multicastEp.address()));
83
84 tx.open(udp::v4());
85 tx.set_option(udp::socket::reuse_address(true));
86 tx.set_option(ip::multicast::enable_loopback(true));
87 tx.bind(udp::endpoint(ip::address_v4::any(), port));
88 }
89
90 void
91 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
92 {
93 remoteSockRx.async_receive(boost::asio::buffer(buf),
94 [this, needToCheck] (const boost::system::error_code& error, size_t) {
95 if (needToCheck) {
96 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
97 }
98 limitedIo.afterOp();
99 });
100 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
101 }
102
103 void
104 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
105 {
106 remoteSockTx.async_send_to(boost::asio::buffer(buf), udp::endpoint(multicastEp.address(), 7001),
107 [needToCheck] (const boost::system::error_code& error, size_t) {
108 if (needToCheck) {
109 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
110 }
111 });
112 limitedIo.defer(time::seconds(1));
113 }
114
115protected:
116 LimitedIo limitedIo;
117 MulticastUdpTransport* transport;
118 udp::endpoint localEp;
119 udp::endpoint multicastEp;
120 const ip::address_v4 defaultAddr;
121 std::vector<Transport::Packet>* receivedPackets;
122
123private:
124 unique_ptr<Face> face;
125 udp::socket remoteSockRx;
126 udp::socket remoteSockTx;
127};
128
129} // namespace tests
130} // namespace face
131} // namespace nfd
132
133#endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP