Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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_UNIX_STREAM_TRANSPORT_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_UNIX_STREAM_TRANSPORT_FIXTURE_HPP |
| 28 | |
| 29 | #include "face/unix-stream-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 | |
| 37 | #include <boost/filesystem.hpp> |
| 38 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 39 | namespace nfd::tests { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 41 | using unix_stream = boost::asio::local::stream_protocol; |
| 42 | using face::UnixStreamTransport; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 43 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 44 | /** |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 45 | * \brief Automatically unlinks the socket file of a Unix stream acceptor. |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 46 | */ |
| 47 | class AcceptorWithCleanup : public unix_stream::acceptor |
| 48 | { |
| 49 | public: |
| 50 | explicit |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 51 | AcceptorWithCleanup(boost::asio::io_context& io, const std::string& path = "") |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 52 | : unix_stream::acceptor(io) |
| 53 | { |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 54 | open(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 55 | |
| 56 | if (path.empty()) { |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 57 | bind("nfd-unix-stream-test." + std::to_string(time::system_clock::now().time_since_epoch().count()) + ".sock"); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 58 | } |
| 59 | else { |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 60 | bind(path); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 63 | listen(1); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | ~AcceptorWithCleanup() |
| 67 | { |
| 68 | boost::system::error_code ec; |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 69 | std::string path = local_endpoint(ec).path(); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 70 | if (ec) { |
| 71 | return; |
| 72 | } |
| 73 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 74 | close(ec); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 75 | boost::filesystem::remove(path, ec); |
| 76 | } |
| 77 | }; |
| 78 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 79 | class UnixStreamTransportFixture : public GlobalIoFixture |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 80 | { |
| 81 | protected: |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 82 | [[nodiscard]] std::tuple<bool, std::string> |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 83 | checkPreconditions() const |
| 84 | { |
| 85 | return {true, ""}; |
| 86 | } |
| 87 | |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 88 | void |
| 89 | initialize() |
| 90 | { |
| 91 | unix_stream::socket sock(g_io); |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame] | 92 | m_acceptor.async_accept(sock, [this] (const auto& error) { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 93 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 94 | limitedIo.afterOp(); |
| 95 | }); |
| 96 | |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame] | 97 | unix_stream::endpoint remoteEp(m_acceptor.local_endpoint()); |
| 98 | remoteSocket.async_connect(remoteEp, [this] (const auto& error) { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 99 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 100 | limitedIo.afterOp(); |
| 101 | }); |
| 102 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 103 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 104 | |
| 105 | localEp = sock.local_endpoint(); |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame] | 106 | m_face = make_unique<Face>(make_unique<DummyLinkService>(), |
| 107 | make_unique<UnixStreamTransport>(std::move(sock))); |
| 108 | transport = static_cast<UnixStreamTransport*>(m_face->getTransport()); |
| 109 | receivedPackets = &static_cast<DummyLinkService*>(m_face->getLinkService())->receivedPackets; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 110 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 111 | BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void |
| 115 | remoteWrite(const ndn::Buffer& buf, bool needToCheck = true) |
| 116 | { |
| 117 | boost::asio::async_write(remoteSocket, boost::asio::buffer(buf), |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 118 | [needToCheck] (const auto& error, size_t) { |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 119 | if (needToCheck) { |
| 120 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 121 | } |
| 122 | }); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 123 | limitedIo.defer(1_s); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | protected: |
| 127 | LimitedIo limitedIo; |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame] | 128 | UnixStreamTransport* transport = nullptr; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 129 | unix_stream::endpoint localEp; |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame] | 130 | unix_stream::socket remoteSocket{g_io}; |
| 131 | std::vector<RxPacket>* receivedPackets = nullptr; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 132 | |
| 133 | private: |
Davide Pesavento | ef6a528 | 2023-11-03 16:50:10 -0400 | [diff] [blame] | 134 | AcceptorWithCleanup m_acceptor{g_io}; |
| 135 | unique_ptr<Face> m_face; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 138 | } // namespace nfd::tests |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 139 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 140 | #endif // NFD_TESTS_DAEMON_FACE_UNIX_STREAM_TRANSPORT_FIXTURE_HPP |