Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 2 | /* |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 4 | * 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 Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 33 | #include "test-netif-ip.hpp" |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 34 | #include "tests/limited-io.hpp" |
| 35 | |
| 36 | namespace nfd { |
| 37 | namespace face { |
| 38 | namespace tests { |
| 39 | |
| 40 | using namespace nfd::tests; |
| 41 | namespace ip = boost::asio::ip; |
| 42 | using ip::udp; |
| 43 | |
| 44 | class MulticastUdpTransportFixture : public BaseFixture |
| 45 | { |
| 46 | protected: |
| 47 | MulticastUdpTransportFixture() |
| 48 | : transport(nullptr) |
| 49 | , multicastEp(ip::address::from_string("230.15.19.47"), 7070) |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 50 | , defaultAddr(getTestIp(AddressFamily::V4, AddressScope::Global, MulticastInterface::Yes)) |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 51 | , receivedPackets(nullptr) |
| 52 | , remoteSockRx(g_io) |
| 53 | , remoteSockTx(g_io) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 58 | initialize(ip::address address) |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 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 Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 66 | ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 67 | |
| 68 | face = make_unique<Face>( |
| 69 | make_unique<DummyReceiveLinkService>(), |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 70 | make_unique<MulticastUdpTransport>(localEp, multicastEp, std::move(sockRx), |
| 71 | std::move(sockTx), linkType)); |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 72 | 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 | |
| 117 | protected: |
| 118 | LimitedIo limitedIo; |
| 119 | MulticastUdpTransport* transport; |
| 120 | udp::endpoint localEp; |
| 121 | udp::endpoint multicastEp; |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 122 | const ip::address defaultAddr; |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 123 | std::vector<Transport::Packet>* receivedPackets; |
| 124 | |
| 125 | private: |
| 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 |