blob: 605597d4947a6532de7e5ccdd26473c68774fbf4 [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());
Davide Pesavento4b1921f2024-01-12 20:25:45 -050045 }
46
47 ~UnixStreamChannelFixture() override
48 {
Davide Pesavento4b1921f2024-01-12 20:25:45 -050049 boost::system::error_code ec;
Davide Pesavento152874a2024-02-20 22:07:07 -050050 fs::remove_all(testDir, ec); // ignore error
Spencer Lee75142a12016-04-13 16:55:10 -070051 }
52
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040053 shared_ptr<UnixStreamChannel>
Davide Pesavento9a00fab2016-09-27 11:22:46 +020054 makeChannel() final
Spencer Lee75142a12016-04-13 16:55:10 -070055 {
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040056 return std::make_shared<UnixStreamChannel>(listenerEp, false);
Spencer Lee75142a12016-04-13 16:55:10 -070057 }
58
59 void
60 listen()
61 {
62 listenerChannel = makeChannel();
63 listenerChannel->listen(
64 [this] (const shared_ptr<Face>& newFace) {
65 BOOST_REQUIRE(newFace != nullptr);
66 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
67 listenerFaces.push_back(newFace);
68 limitedIo.afterOp();
69 },
Davide Pesavento9a00fab2016-09-27 11:22:46 +020070 ChannelFixture::unexpectedFailure);
Spencer Lee75142a12016-04-13 16:55:10 -070071 }
72
73 void
Davide Pesavento9a00fab2016-09-27 11:22:46 +020074 clientConnect(local::stream_protocol::socket& client)
Spencer Lee75142a12016-04-13 16:55:10 -070075 {
76 client.async_connect(listenerEp,
Davide Pesavento4c957712024-01-01 15:40:06 -050077 [this] (const auto& error) {
Spencer Lee75142a12016-04-13 16:55:10 -070078 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
79 limitedIo.afterOp();
80 });
81 }
Davide Pesavento4c957712024-01-01 15:40:06 -050082
83protected:
Davide Pesavento152874a2024-02-20 22:07:07 -050084 static inline const fs::path testDir = fs::path(UNIT_TESTS_TMPDIR) / "unix-stream-channel";
85 static inline const fs::path socketPath = testDir / "test" / "foo.sock";
Spencer Lee75142a12016-04-13 16:55:10 -070086};
87
Davide Pesavento9a00fab2016-09-27 11:22:46 +020088BOOST_AUTO_TEST_SUITE(Face)
Spencer Lee75142a12016-04-13 16:55:10 -070089BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture)
90
Davide Pesavento9a00fab2016-09-27 11:22:46 +020091BOOST_AUTO_TEST_CASE(Uri)
92{
93 auto channel = makeChannel();
94 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(listenerEp));
95}
96
Spencer Lee75142a12016-04-13 16:55:10 -070097BOOST_AUTO_TEST_CASE(Listen)
98{
99 auto channel = makeChannel();
100 BOOST_CHECK_EQUAL(channel->isListening(), false);
101
102 channel->listen(nullptr, nullptr);
103 BOOST_CHECK_EQUAL(channel->isListening(), true);
104
105 // listen() is idempotent
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500106 channel->listen(nullptr, nullptr);
Spencer Lee75142a12016-04-13 16:55:10 -0700107 BOOST_CHECK_EQUAL(channel->isListening(), true);
108}
109
110BOOST_AUTO_TEST_CASE(MultipleAccepts)
111{
112 this->listen();
113
114 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400115 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
Spencer Lee75142a12016-04-13 16:55:10 -0700116
117 local::stream_protocol::socket client1(g_io);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200118 this->clientConnect(client1);
Spencer Lee75142a12016-04-13 16:55:10 -0700119
Davide Pesavento14e71f02019-03-28 17:35:25 -0400120 BOOST_CHECK_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400121 BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
Spencer Lee75142a12016-04-13 16:55:10 -0700122
123 local::stream_protocol::socket client2(g_io);
124 local::stream_protocol::socket client3(g_io);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200125 this->clientConnect(client2);
126 this->clientConnect(client3);
Spencer Lee75142a12016-04-13 16:55:10 -0700127
Davide Pesavento14e71f02019-03-28 17:35:25 -0400128 BOOST_CHECK_EQUAL(limitedIo.run(4, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400129 BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
Spencer Lee75142a12016-04-13 16:55:10 -0700130 BOOST_CHECK_EQUAL(listenerFaces.size(), 3);
131
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400132 // check face persistency and channel association
Spencer Lee75142a12016-04-13 16:55:10 -0700133 for (const auto& face : listenerFaces) {
134 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400135 BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel);
Spencer Lee75142a12016-04-13 16:55:10 -0700136 }
137}
138
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500139BOOST_AUTO_TEST_SUITE(SocketFile)
140
141BOOST_AUTO_TEST_CASE(CreateAndRemove)
Spencer Lee75142a12016-04-13 16:55:10 -0700142{
Spencer Lee75142a12016-04-13 16:55:10 -0700143 auto channel = makeChannel();
144 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found);
145
146 channel->listen(nullptr, nullptr);
147 auto status = fs::symlink_status(socketPath);
148 BOOST_CHECK_EQUAL(status.type(), fs::socket_file);
149 BOOST_CHECK_EQUAL(status.permissions(), fs::owner_read | fs::group_read | fs::others_read |
150 fs::owner_write | fs::group_write | fs::others_write);
151
152 channel.reset();
153 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found);
154}
155
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500156BOOST_AUTO_TEST_CASE(InUse)
Spencer Lee75142a12016-04-13 16:55:10 -0700157{
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500158 auto channel = makeChannel();
Davide Pesavento4c957712024-01-01 15:40:06 -0500159 fs::create_directories(socketPath.parent_path());
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500160
Spencer Lee75142a12016-04-13 16:55:10 -0700161 local::stream_protocol::acceptor acceptor(g_io, listenerEp);
Spencer Lee75142a12016-04-13 16:55:10 -0700162 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
163
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500164 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
165 return e.code() == boost::system::errc::address_in_use &&
166 e.path1() == socketPath &&
167 std::string_view(e.what()).find("UnixStreamChannel::listen") != std::string_view::npos;
168 });
169}
Davide Pesavento4c957712024-01-01 15:40:06 -0500170
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500171BOOST_AUTO_TEST_CASE(Stale)
172{
173 auto channel = makeChannel();
174 fs::create_directories(socketPath.parent_path());
175
176 local::stream_protocol::acceptor acceptor(g_io, listenerEp);
Davide Pesavento4c957712024-01-01 15:40:06 -0500177 acceptor.close();
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500178 // the socket file is not removed when the acceptor is closed
Davide Pesavento4c957712024-01-01 15:40:06 -0500179 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
180
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500181 // drop write permission from the parent directory
182 fs::permissions(socketPath.parent_path(), fs::owner_all & ~fs::owner_write);
183 // removal of the "stale" socket fails due to insufficient permissions
184 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
185 return e.code() == boost::system::errc::permission_denied &&
186 e.path1() == socketPath &&
187 std::string_view(e.what()).find("remove") != std::string_view::npos;
188 });
189 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
190
191 // restore all permissions
192 fs::permissions(socketPath.parent_path(), fs::owner_all);
193 // the socket file should be considered "stale" and overwritten
194 channel->listen(nullptr, nullptr);
Spencer Lee75142a12016-04-13 16:55:10 -0700195 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
196}
197
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500198BOOST_AUTO_TEST_CASE(NotASocket)
Spencer Lee75142a12016-04-13 16:55:10 -0700199{
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500200 auto channel = makeChannel();
Spencer Lee75142a12016-04-13 16:55:10 -0700201
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500202 fs::create_directories(socketPath);
203 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::directory_file);
204 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
205 return e.code() == boost::system::errc::not_a_socket &&
206 e.path1() == socketPath &&
207 std::string_view(e.what()).find("UnixStreamChannel::listen") != std::string_view::npos;
208 });
209
210 fs::remove(socketPath);
Davide Pesavento4c957712024-01-01 15:40:06 -0500211 std::ofstream f(socketPath.string());
Spencer Lee75142a12016-04-13 16:55:10 -0700212 f.close();
Davide Pesavento4c957712024-01-01 15:40:06 -0500213 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::regular_file);
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500214 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
215 return e.code() == boost::system::errc::not_a_socket &&
216 e.path1() == socketPath &&
217 std::string_view(e.what()).find("UnixStreamChannel::listen") != std::string_view::npos;
218 });
Spencer Lee75142a12016-04-13 16:55:10 -0700219}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700220
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500221BOOST_AUTO_TEST_CASE(ParentConflict)
222{
223 auto channel = makeChannel();
224 fs::create_directories(testDir);
225
226 auto parent = socketPath.parent_path();
227 std::ofstream f(parent.string());
228 f.close();
229 BOOST_CHECK_EQUAL(fs::symlink_status(parent).type(), fs::regular_file);
230 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
231 return e.code() == boost::system::errc::file_exists &&
232 e.path1() == parent &&
233 std::string_view(e.what()).find("create_dir") != std::string_view::npos;
234 });
235}
236
237BOOST_AUTO_TEST_CASE(PermissionDenied)
238{
239 auto channel = makeChannel();
240 fs::create_directories(testDir);
241
242 fs::permissions(testDir, fs::no_perms);
243 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
244 return e.code() == boost::system::errc::permission_denied &&
245 e.path1() == socketPath.parent_path() &&
246 std::string_view(e.what()).find("create_dir") != std::string_view::npos;
247 });
248
249 fs::permissions(testDir, fs::owner_read | fs::owner_exe);
250 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
251 return e.code() == boost::system::errc::permission_denied &&
252 e.path1() == socketPath.parent_path() &&
253 std::string_view(e.what()).find("create_dir") != std::string_view::npos;
254 });
255
256 fs::permissions(testDir, fs::owner_all);
257 fs::create_directories(socketPath.parent_path());
258
259 fs::permissions(socketPath.parent_path(), fs::no_perms);
260 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
261 return e.code() == boost::system::errc::permission_denied &&
262 e.path1() == socketPath &&
263 std::string_view(e.what()).find("status") != std::string_view::npos;
264 });
265
266 fs::permissions(socketPath.parent_path(), fs::owner_read | fs::owner_exe);
267 BOOST_CHECK_EXCEPTION(channel->listen(nullptr, nullptr), fs::filesystem_error, [&] (const auto& e) {
268 return e.code() == boost::system::errc::permission_denied &&
269 e.path1() == socketPath &&
270 std::string_view(e.what()).find("bind") != std::string_view::npos;
271 });
272
273 fs::permissions(socketPath.parent_path(), fs::owner_all);
274}
275
276BOOST_AUTO_TEST_SUITE_END() // SocketFile
277
Junxiao Shicde37ad2015-12-24 01:02:05 -0700278BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamChannel
279BOOST_AUTO_TEST_SUITE_END() // Face
280
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400281} // namespace nfd::tests