blob: 43b1ad05072e2da0aa7206e7a3fc07dc6f2e8dd7 [file] [log] [blame]
Eric Newberryf68f3782015-12-11 00:31:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento22fba352017-10-17 15:53:51 -04002/*
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Eric Newberryf68f3782015-12-11 00:31:32 -07004 * 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 Shicde37ad2015-12-24 01:02:05 -070030#include "face/face.hpp"
Eric Newberryf68f3782015-12-11 00:31:32 -070031
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040032#include "tests/test-common.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060033#include "tests/daemon/limited-io.hpp"
Davide Pesaventoa8098582019-03-31 15:48:02 -040034#include "tests/daemon/face/dummy-link-service.hpp"
Davide Pesavento279af1c2022-08-29 20:18:32 -040035#include "tests/daemon/face/transport-test-common.hpp"
Eric Newberryf68f3782015-12-11 00:31:32 -070036
37#include <boost/filesystem.hpp>
38
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040039namespace nfd::tests {
Eric Newberryf68f3782015-12-11 00:31:32 -070040
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040041using unix_stream = boost::asio::local::stream_protocol;
42using face::UnixStreamTransport;
Eric Newberryf68f3782015-12-11 00:31:32 -070043
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040044/**
45 * \brief Automatically unlinks the socket file of a Unix stream acceptor
Eric Newberryf68f3782015-12-11 00:31:32 -070046 */
47class AcceptorWithCleanup : public unix_stream::acceptor
48{
49public:
50 explicit
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040051 AcceptorWithCleanup(boost::asio::io_context& io, const std::string& path = "")
Eric Newberryf68f3782015-12-11 00:31:32 -070052 : unix_stream::acceptor(io)
53 {
54 this->open();
55
56 if (path.empty()) {
57 this->bind("nfd-unix-stream-test." + to_string(time::system_clock::now().time_since_epoch().count()) + ".sock");
58 }
59 else {
60 this->bind(path);
61 }
62
63 this->listen(1);
64 }
65
66 ~AcceptorWithCleanup()
67 {
68 boost::system::error_code ec;
69
70 std::string path = this->local_endpoint(ec).path();
71 if (ec) {
72 return;
73 }
74
75 this->close(ec);
76 boost::filesystem::remove(path, ec);
77 }
78};
79
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040080class UnixStreamTransportFixture : public GlobalIoFixture
Eric Newberryf68f3782015-12-11 00:31:32 -070081{
82protected:
Davide Pesavento279af1c2022-08-29 20:18:32 -040083 [[nodiscard]] std::tuple<bool, std::string>
Davide Pesavento22fba352017-10-17 15:53:51 -040084 checkPreconditions() const
85 {
86 return {true, ""};
87 }
88
Eric Newberryf68f3782015-12-11 00:31:32 -070089 void
90 initialize()
91 {
92 unix_stream::socket sock(g_io);
Davide Pesaventoef6a5282023-11-03 16:50:10 -040093 m_acceptor.async_accept(sock, [this] (const auto& error) {
Eric Newberryf68f3782015-12-11 00:31:32 -070094 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
95 limitedIo.afterOp();
96 });
97
Davide Pesaventoef6a5282023-11-03 16:50:10 -040098 unix_stream::endpoint remoteEp(m_acceptor.local_endpoint());
99 remoteSocket.async_connect(remoteEp, [this] (const auto& error) {
Eric Newberryf68f3782015-12-11 00:31:32 -0700100 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
101 limitedIo.afterOp();
102 });
103
Davide Pesavento14e71f02019-03-28 17:35:25 -0400104 BOOST_REQUIRE_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -0700105
106 localEp = sock.local_endpoint();
Davide Pesaventoef6a5282023-11-03 16:50:10 -0400107 m_face = make_unique<Face>(make_unique<DummyLinkService>(),
108 make_unique<UnixStreamTransport>(std::move(sock)));
109 transport = static_cast<UnixStreamTransport*>(m_face->getTransport());
110 receivedPackets = &static_cast<DummyLinkService*>(m_face->getLinkService())->receivedPackets;
Eric Newberryf68f3782015-12-11 00:31:32 -0700111
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400112 BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
Eric Newberryf68f3782015-12-11 00:31:32 -0700113 }
114
115 void
116 remoteWrite(const ndn::Buffer& buf, bool needToCheck = true)
117 {
118 boost::asio::async_write(remoteSocket, boost::asio::buffer(buf),
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400119 [needToCheck] (const auto& error, size_t) {
Eric Newberryf68f3782015-12-11 00:31:32 -0700120 if (needToCheck) {
121 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
122 }
123 });
Davide Pesavento14e71f02019-03-28 17:35:25 -0400124 limitedIo.defer(1_s);
Eric Newberryf68f3782015-12-11 00:31:32 -0700125 }
126
127protected:
128 LimitedIo limitedIo;
Davide Pesaventoef6a5282023-11-03 16:50:10 -0400129 UnixStreamTransport* transport = nullptr;
Eric Newberryf68f3782015-12-11 00:31:32 -0700130 unix_stream::endpoint localEp;
Davide Pesaventoef6a5282023-11-03 16:50:10 -0400131 unix_stream::socket remoteSocket{g_io};
132 std::vector<RxPacket>* receivedPackets = nullptr;
Eric Newberryf68f3782015-12-11 00:31:32 -0700133
134private:
Davide Pesaventoef6a5282023-11-03 16:50:10 -0400135 AcceptorWithCleanup m_acceptor{g_io};
136 unique_ptr<Face> m_face;
Eric Newberryf68f3782015-12-11 00:31:32 -0700137};
138
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400139} // namespace nfd::tests
Eric Newberryf68f3782015-12-11 00:31:32 -0700140
Eric Newberry65caf202015-12-17 15:08:16 -0700141#endif // NFD_TESTS_DAEMON_FACE_UNIX_STREAM_TRANSPORT_FIXTURE_HPP