blob: d58362519ead54c276da1d79f7546b2c19f69bb1 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry0c841642018-01-17 15:01:00 -07002/*
Davide Pesavento4c957712024-01-01 15:40:06 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -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
Junxiao Shicde37ad2015-12-24 01:02:05 -070026#include "face/unix-stream-channel.hpp"
27
Davide Pesavento9a00fab2016-09-27 11:22:46 +020028#include "channel-fixture.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070029
Spencer Lee75142a12016-04-13 16:55:10 -070030#include <boost/filesystem.hpp>
31#include <fstream>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
Eric Newberrya98bf932015-09-21 00:58:47 -070034
Spencer Lee75142a12016-04-13 16:55:10 -070035namespace fs = boost::filesystem;
36namespace local = boost::asio::local;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037using face::UnixStreamChannel;
Junxiao Shicde37ad2015-12-24 01:02:05 -070038
Davide Pesavento9a00fab2016-09-27 11:22:46 +020039class UnixStreamChannelFixture : public ChannelFixture<UnixStreamChannel, unix_stream::Endpoint>
Spencer Lee75142a12016-04-13 16:55:10 -070040{
41protected:
42 UnixStreamChannelFixture()
Spencer Lee75142a12016-04-13 16:55:10 -070043 {
Davide Pesavento4c957712024-01-01 15:40:06 -050044 listenerEp = unix_stream::Endpoint(socketPath.string());
45 fs::remove_all(socketPath.parent_path());
Spencer Lee75142a12016-04-13 16:55:10 -070046 }
47
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040048 shared_ptr<UnixStreamChannel>
Davide Pesavento9a00fab2016-09-27 11:22:46 +020049 makeChannel() final
Spencer Lee75142a12016-04-13 16:55:10 -070050 {
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040051 return std::make_shared<UnixStreamChannel>(listenerEp, false);
Spencer Lee75142a12016-04-13 16:55:10 -070052 }
53
54 void
55 listen()
56 {
57 listenerChannel = makeChannel();
58 listenerChannel->listen(
59 [this] (const shared_ptr<Face>& newFace) {
60 BOOST_REQUIRE(newFace != nullptr);
61 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
62 listenerFaces.push_back(newFace);
63 limitedIo.afterOp();
64 },
Davide Pesavento9a00fab2016-09-27 11:22:46 +020065 ChannelFixture::unexpectedFailure);
Spencer Lee75142a12016-04-13 16:55:10 -070066 }
67
68 void
Davide Pesavento9a00fab2016-09-27 11:22:46 +020069 clientConnect(local::stream_protocol::socket& client)
Spencer Lee75142a12016-04-13 16:55:10 -070070 {
71 client.async_connect(listenerEp,
Davide Pesavento4c957712024-01-01 15:40:06 -050072 [this] (const auto& error) {
Spencer Lee75142a12016-04-13 16:55:10 -070073 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
74 limitedIo.afterOp();
75 });
76 }
Davide Pesavento4c957712024-01-01 15:40:06 -050077
78protected:
79 fs::path socketPath = fs::path(UNIT_TESTS_TMPDIR) / "unix-stream-channel" / "test" / "foo.sock";
Spencer Lee75142a12016-04-13 16:55:10 -070080};
81
Davide Pesavento9a00fab2016-09-27 11:22:46 +020082BOOST_AUTO_TEST_SUITE(Face)
Spencer Lee75142a12016-04-13 16:55:10 -070083BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture)
84
Davide Pesavento9a00fab2016-09-27 11:22:46 +020085BOOST_AUTO_TEST_CASE(Uri)
86{
87 auto channel = makeChannel();
88 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(listenerEp));
89}
90
Spencer Lee75142a12016-04-13 16:55:10 -070091BOOST_AUTO_TEST_CASE(Listen)
92{
93 auto channel = makeChannel();
94 BOOST_CHECK_EQUAL(channel->isListening(), false);
95
96 channel->listen(nullptr, nullptr);
97 BOOST_CHECK_EQUAL(channel->isListening(), true);
98
99 // listen() is idempotent
100 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
101 BOOST_CHECK_EQUAL(channel->isListening(), true);
102}
103
104BOOST_AUTO_TEST_CASE(MultipleAccepts)
105{
106 this->listen();
107
108 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400109 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
Spencer Lee75142a12016-04-13 16:55:10 -0700110
111 local::stream_protocol::socket client1(g_io);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200112 this->clientConnect(client1);
Spencer Lee75142a12016-04-13 16:55:10 -0700113
Davide Pesavento14e71f02019-03-28 17:35:25 -0400114 BOOST_CHECK_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400115 BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
Spencer Lee75142a12016-04-13 16:55:10 -0700116
117 local::stream_protocol::socket client2(g_io);
118 local::stream_protocol::socket client3(g_io);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200119 this->clientConnect(client2);
120 this->clientConnect(client3);
Spencer Lee75142a12016-04-13 16:55:10 -0700121
Davide Pesavento14e71f02019-03-28 17:35:25 -0400122 BOOST_CHECK_EQUAL(limitedIo.run(4, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400123 BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
Spencer Lee75142a12016-04-13 16:55:10 -0700124 BOOST_CHECK_EQUAL(listenerFaces.size(), 3);
125
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400126 // check face persistency and channel association
Spencer Lee75142a12016-04-13 16:55:10 -0700127 for (const auto& face : listenerFaces) {
128 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400129 BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel);
Spencer Lee75142a12016-04-13 16:55:10 -0700130 }
131}
132
133BOOST_AUTO_TEST_CASE(SocketFile)
134{
Spencer Lee75142a12016-04-13 16:55:10 -0700135 auto channel = makeChannel();
136 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found);
137
138 channel->listen(nullptr, nullptr);
139 auto status = fs::symlink_status(socketPath);
140 BOOST_CHECK_EQUAL(status.type(), fs::socket_file);
141 BOOST_CHECK_EQUAL(status.permissions(), fs::owner_read | fs::group_read | fs::others_read |
142 fs::owner_write | fs::group_write | fs::others_write);
143
144 channel.reset();
145 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found);
146}
147
Davide Pesavento4c957712024-01-01 15:40:06 -0500148BOOST_AUTO_TEST_CASE(ExistingSocketFile)
Spencer Lee75142a12016-04-13 16:55:10 -0700149{
Davide Pesavento4c957712024-01-01 15:40:06 -0500150 fs::create_directories(socketPath.parent_path());
Spencer Lee75142a12016-04-13 16:55:10 -0700151 local::stream_protocol::acceptor acceptor(g_io, listenerEp);
Spencer Lee75142a12016-04-13 16:55:10 -0700152 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
153
154 auto channel = makeChannel();
Davide Pesavento4c957712024-01-01 15:40:06 -0500155 BOOST_CHECK_THROW(channel->listen(nullptr, nullptr), UnixStreamChannel::Error);
156
157 acceptor.close();
158 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
159
Spencer Lee75142a12016-04-13 16:55:10 -0700160 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
161 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
162}
163
164BOOST_AUTO_TEST_CASE(ExistingRegularFile)
165{
Davide Pesavento4c957712024-01-01 15:40:06 -0500166 auto guard = ndn::make_scope_exit([=] { fs::remove(socketPath); });
Spencer Lee75142a12016-04-13 16:55:10 -0700167
Davide Pesavento4c957712024-01-01 15:40:06 -0500168 fs::create_directories(socketPath.parent_path());
169 std::ofstream f(socketPath.string());
Spencer Lee75142a12016-04-13 16:55:10 -0700170 f.close();
Davide Pesavento4c957712024-01-01 15:40:06 -0500171 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::regular_file);
Spencer Lee75142a12016-04-13 16:55:10 -0700172
173 auto channel = makeChannel();
174 BOOST_CHECK_THROW(channel->listen(nullptr, nullptr), UnixStreamChannel::Error);
Spencer Lee75142a12016-04-13 16:55:10 -0700175}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700176
177BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamChannel
178BOOST_AUTO_TEST_SUITE_END() // Face
179
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400180} // namespace nfd::tests