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