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 | /* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 32 | #include "tests/test-common.hpp" |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 33 | #include "tests/daemon/limited-io.hpp" |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame] | 34 | #include "tests/daemon/face/dummy-link-service.hpp" |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 44 | class MulticastUdpTransportFixture : public GlobalIoFixture |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 45 | { |
| 46 | protected: |
| 47 | MulticastUdpTransportFixture() |
| 48 | : transport(nullptr) |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 49 | , txPort(7001) |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 50 | , receivedPackets(nullptr) |
| 51 | , remoteSockRx(g_io) |
| 52 | , remoteSockTx(g_io) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 57 | initialize(ip::address address) |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 58 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 59 | ip::address mcastAddr; |
| 60 | if (address.is_v4()) { |
| 61 | // the administratively scoped group 224.0.0.254 is reserved for experimentation (RFC 4727) |
| 62 | mcastAddr = ip::address_v4(0xE00000FE); |
| 63 | } |
| 64 | else { |
| 65 | // the group FF0X::114 is reserved for experimentation at all scope levels (RFC 4727) |
| 66 | auto v6Addr = ip::address_v6::from_string("FF01::114"); |
| 67 | v6Addr.scope_id(address.to_v6().scope_id()); |
| 68 | mcastAddr = v6Addr; |
| 69 | } |
| 70 | mcastEp = udp::endpoint(mcastAddr, 7373); |
| 71 | remoteMcastEp = udp::endpoint(mcastAddr, 8383); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 72 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 73 | MulticastUdpTransport::openRxSocket(remoteSockRx, mcastEp, address); |
| 74 | MulticastUdpTransport::openTxSocket(remoteSockTx, udp::endpoint(address, 0), nullptr, true); |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 75 | |
| 76 | udp::socket sockRx(g_io); |
| 77 | udp::socket sockTx(g_io); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 78 | MulticastUdpTransport::openRxSocket(sockRx, remoteMcastEp, address); |
| 79 | MulticastUdpTransport::openTxSocket(sockTx, udp::endpoint(address, txPort), nullptr, true); |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 80 | |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame] | 81 | face = make_unique<Face>(make_unique<DummyLinkService>(), |
| 82 | make_unique<MulticastUdpTransport>(mcastEp, std::move(sockRx), std::move(sockTx), |
| 83 | ndn::nfd::LINK_TYPE_MULTI_ACCESS)); |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 84 | transport = static_cast<MulticastUdpTransport*>(face->getTransport()); |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame] | 85 | receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets; |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 86 | |
| 87 | BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP); |
| 88 | } |
| 89 | |
| 90 | void |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 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 | }); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 100 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
| 104 | remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true) |
| 105 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 106 | sendToGroup(remoteSockTx, buf, needToCheck); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 107 | limitedIo.defer(1_s); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void |
| 111 | sendToGroup(udp::socket& sock, const std::vector<uint8_t>& buf, bool needToCheck = true) const |
| 112 | { |
| 113 | sock.async_send_to(boost::asio::buffer(buf), remoteMcastEp, |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 114 | [needToCheck] (const auto& error, size_t) { |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 115 | if (needToCheck) { |
| 116 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 117 | } |
| 118 | }); |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | protected: |
| 122 | LimitedIo limitedIo; |
| 123 | MulticastUdpTransport* transport; |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 124 | udp::endpoint mcastEp; |
| 125 | uint16_t txPort; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 126 | std::vector<RxPacket>* receivedPackets; |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | unique_ptr<Face> face; |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 130 | udp::endpoint remoteMcastEp; |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 131 | udp::socket remoteSockRx; |
| 132 | udp::socket remoteSockTx; |
| 133 | }; |
| 134 | |
| 135 | } // namespace tests |
| 136 | } // namespace face |
| 137 | } // namespace nfd |
| 138 | |
| 139 | #endif // NFD_TESTS_DAEMON_FACE_MULTICAST_UDP_TRANSPORT_FIXTURE_HPP |