Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -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 | 65caf20 | 2015-12-17 15:08:16 -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_UNICAST_UDP_TRANSPORT_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP |
| 28 | |
| 29 | #include "face/unicast-udp-transport.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | #include "face/face.hpp" |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 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 | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tests { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 37 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -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::UnicastUdpTransport; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 41 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 42 | class UnicastUdpTransportFixture : public GlobalIoFixture |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 43 | { |
| 44 | protected: |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 45 | void |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 46 | initialize(ip::address address, |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 47 | ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 48 | { |
| 49 | udp::socket sock(g_io); |
| 50 | sock.connect(udp::endpoint(address, 7070)); |
| 51 | localEp = sock.local_endpoint(); |
| 52 | |
| 53 | remoteConnect(address); |
| 54 | |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame] | 55 | face = make_unique<Face>(make_unique<DummyLinkService>(), |
| 56 | make_unique<UnicastUdpTransport>(std::move(sock), persistency, 3_s)); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 57 | transport = static_cast<UnicastUdpTransport*>(face->getTransport()); |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame] | 58 | receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 59 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 60 | BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void |
| 64 | remoteConnect(ip::address address = ip::address_v4::loopback()) |
| 65 | { |
| 66 | udp::endpoint remoteEp(address, 7070); |
| 67 | remoteSocket.open(remoteEp.protocol()); |
| 68 | remoteSocket.set_option(udp::socket::reuse_address(true)); |
| 69 | remoteSocket.bind(remoteEp); |
| 70 | remoteSocket.connect(localEp); |
| 71 | } |
| 72 | |
| 73 | void |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 74 | remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true) |
| 75 | { |
| 76 | remoteSocket.async_receive(boost::asio::buffer(buf), |
| 77 | [this, needToCheck] (const boost::system::error_code& error, size_t) { |
| 78 | if (needToCheck) { |
| 79 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 80 | } |
| 81 | limitedIo.afterOp(); |
| 82 | }); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 83 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 87 | remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true) |
| 88 | { |
| 89 | remoteSocket.async_send(boost::asio::buffer(buf), |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 90 | [needToCheck] (const auto& error, size_t) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 91 | if (needToCheck) { |
| 92 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 93 | } |
| 94 | }); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 95 | limitedIo.defer(1_s); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | protected: |
| 99 | LimitedIo limitedIo; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 100 | UnicastUdpTransport* transport = nullptr; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 101 | udp::endpoint localEp; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 102 | udp::socket remoteSocket{g_io}; |
| 103 | std::vector<RxPacket>* receivedPackets = nullptr; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 104 | |
| 105 | private: |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 106 | unique_ptr<Face> face; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 109 | } // namespace nfd::tests |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 110 | |
| 111 | #endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP |