blob: 2b868330d9efec2a4de3fa87741c5dae07585fd4 [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 {
Davide Pesavento8215a3a2017-12-25 19:14:33 -050058 localEp = udp::endpoint(address, 7001);
59
60 MulticastUdpTransport::openRxSocket(remoteSockRx, multicastEp, ip::address_v4::any());
61 MulticastUdpTransport::openTxSocket(remoteSockTx, udp::endpoint(udp::v4(), 0), true);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070062
63 udp::socket sockRx(g_io);
64 udp::socket sockTx(g_io);
Davide Pesavento8215a3a2017-12-25 19:14:33 -050065 MulticastUdpTransport::openRxSocket(sockRx, udp::endpoint(multicastEp.address(), localEp.port()),
66 ip::address_v4::any());
67 MulticastUdpTransport::openTxSocket(sockTx, udp::endpoint(udp::v4(), 0), true);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070068
69 face = make_unique<Face>(
70 make_unique<DummyReceiveLinkService>(),
Teng Liangfe4fce32017-03-29 04:49:38 +000071 make_unique<MulticastUdpTransport>(localEp, multicastEp, std::move(sockRx),
Davide Pesavento22fba352017-10-17 15:53:51 -040072 std::move(sockTx), ndn::nfd::LINK_TYPE_MULTI_ACCESS));
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070073 transport = static_cast<MulticastUdpTransport*>(face->getTransport());
74 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
75
76 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
77 }
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 });
89 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
90 }
91
92 void
93 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
94 {
95 remoteSockTx.async_send_to(boost::asio::buffer(buf), udp::endpoint(multicastEp.address(), 7001),
96 [needToCheck] (const boost::system::error_code& error, size_t) {
97 if (needToCheck) {
98 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
99 }
100 });
101 limitedIo.defer(time::seconds(1));
102 }
103
104protected:
105 LimitedIo limitedIo;
106 MulticastUdpTransport* transport;
107 udp::endpoint localEp;
108 udp::endpoint multicastEp;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700109 std::vector<Transport::Packet>* receivedPackets;
110
111private:
112 unique_ptr<Face> face;
113 udp::socket remoteSockRx;
114 udp::socket remoteSockTx;
115};
116
117} // namespace tests
118} // namespace face
119} // namespace nfd
120
121#endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP