Eric Newberry | f68f378 | 2015-12-11 00:31:32 -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 | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -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_TCP_TRANSPORT_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP |
| 28 | |
| 29 | #include "face/tcp-transport.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | #include "face/face.hpp" |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -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" |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 35 | #include "tests/daemon/face/transport-test-common.hpp" |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | namespace nfd::tests { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 38 | |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 39 | namespace ip = boost::asio::ip; |
| 40 | using ip::tcp; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 41 | using face::TcpTransport; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 42 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 43 | class TcpTransportFixture : public GlobalIoFixture |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 44 | { |
| 45 | protected: |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 46 | void |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 47 | startAccept(const tcp::endpoint& remoteEp) |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 48 | { |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame^] | 49 | BOOST_REQUIRE(!m_acceptor.is_open()); |
| 50 | m_acceptor.open(remoteEp.protocol()); |
| 51 | m_acceptor.set_option(boost::asio::socket_base::reuse_address(true)); |
| 52 | m_acceptor.bind(remoteEp); |
| 53 | m_acceptor.listen(1); |
| 54 | m_acceptor.async_accept(remoteSocket, [this] (const auto& error) { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 55 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 56 | limitedIo.afterOp(); |
| 57 | }); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
| 61 | stopAccept() |
| 62 | { |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame^] | 63 | BOOST_REQUIRE(m_acceptor.is_open()); |
| 64 | m_acceptor.close(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 68 | initialize(const shared_ptr<const ndn::net::NetworkInterface>&, const ip::address& address, |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 69 | ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 70 | { |
| 71 | tcp::endpoint remoteEp(address, 7070); |
| 72 | startAccept(remoteEp); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 73 | |
| 74 | tcp::socket sock(g_io); |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame^] | 75 | sock.async_connect(remoteEp, [this] (const auto& error) { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 76 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 77 | limitedIo.afterOp(); |
| 78 | }); |
| 79 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 80 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 81 | |
| 82 | localEp = sock.local_endpoint(); |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 83 | |
| 84 | ndn::nfd::FaceScope scope; |
| 85 | if (sock.local_endpoint().address().is_loopback() && sock.remote_endpoint().address().is_loopback()) { |
| 86 | scope = ndn::nfd::FACE_SCOPE_LOCAL; |
| 87 | } |
| 88 | else { |
| 89 | scope = ndn::nfd::FACE_SCOPE_NON_LOCAL; |
| 90 | } |
| 91 | |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame^] | 92 | m_face = make_unique<Face>(make_unique<DummyLinkService>(), |
| 93 | make_unique<TcpTransport>(std::move(sock), persistency, scope)); |
| 94 | transport = static_cast<TcpTransport*>(m_face->getTransport()); |
| 95 | receivedPackets = &static_cast<DummyLinkService*>(m_face->getLinkService())->receivedPackets; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 96 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 97 | BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 101 | remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true) |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 102 | { |
| 103 | boost::asio::async_write(remoteSocket, boost::asio::buffer(buf), |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 104 | [needToCheck] (const auto& error, size_t) { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 105 | if (needToCheck) { |
| 106 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 107 | } |
| 108 | }); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 109 | limitedIo.defer(1_s); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | protected: |
| 113 | LimitedIo limitedIo; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 114 | TcpTransport* transport = nullptr; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 115 | tcp::endpoint localEp; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 116 | tcp::socket remoteSocket{g_io}; |
| 117 | std::vector<RxPacket>* receivedPackets = nullptr; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 118 | |
| 119 | private: |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame^] | 120 | tcp::acceptor m_acceptor{g_io}; |
| 121 | unique_ptr<Face> m_face; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 124 | } // namespace nfd::tests |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 125 | |
| 126 | #endif // NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP |