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