Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -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 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 26 | #include "face/unix-stream-channel.hpp" |
| 27 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 28 | #include "channel-fixture.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 29 | |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 30 | #include <boost/filesystem.hpp> |
| 31 | #include <fstream> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 34 | |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 35 | namespace fs = boost::filesystem; |
| 36 | namespace local = boost::asio::local; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | using face::UnixStreamChannel; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 39 | class UnixStreamChannelFixture : public ChannelFixture<UnixStreamChannel, unix_stream::Endpoint> |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 40 | { |
| 41 | protected: |
| 42 | UnixStreamChannelFixture() |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 43 | { |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 44 | listenerEp = unix_stream::Endpoint(socketPath.string()); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | ~UnixStreamChannelFixture() override |
| 48 | { |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 49 | boost::system::error_code ec; |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 50 | fs::remove_all(testDir, ec); // ignore error |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 53 | shared_ptr<UnixStreamChannel> |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 54 | makeChannel() final |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 55 | { |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 56 | return std::make_shared<UnixStreamChannel>(listenerEp, false); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 57 | } |
| 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 Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 70 | ChannelFixture::unexpectedFailure); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 74 | clientConnect(local::stream_protocol::socket& client) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 75 | { |
| 76 | client.async_connect(listenerEp, |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 77 | [this] (const auto& error) { |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 78 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 79 | limitedIo.afterOp(); |
| 80 | }); |
| 81 | } |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 82 | |
| 83 | protected: |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 84 | 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 Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 88 | BOOST_AUTO_TEST_SUITE(Face) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 89 | BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture) |
| 90 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 91 | BOOST_AUTO_TEST_CASE(Uri) |
| 92 | { |
| 93 | auto channel = makeChannel(); |
| 94 | BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(listenerEp)); |
| 95 | } |
| 96 | |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 97 | BOOST_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 Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 106 | channel->listen(nullptr, nullptr); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(channel->isListening(), true); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(MultipleAccepts) |
| 111 | { |
| 112 | this->listen(); |
| 113 | |
| 114 | BOOST_CHECK_EQUAL(listenerChannel->isListening(), true); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 116 | |
| 117 | local::stream_protocol::socket client1(g_io); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 118 | this->clientConnect(client1); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 119 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 122 | |
| 123 | local::stream_protocol::socket client2(g_io); |
| 124 | local::stream_protocol::socket client3(g_io); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 125 | this->clientConnect(client2); |
| 126 | this->clientConnect(client3); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 127 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(limitedIo.run(4, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(listenerFaces.size(), 3); |
| 131 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 132 | // check face persistency and channel association |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 133 | for (const auto& face : listenerFaces) { |
| 134 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 135 | BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 139 | BOOST_AUTO_TEST_SUITE(SocketFile) |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(CreateAndRemove) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 142 | { |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 143 | 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 Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 156 | BOOST_AUTO_TEST_CASE(InUse) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 157 | { |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 158 | auto channel = makeChannel(); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 159 | fs::create_directories(socketPath.parent_path()); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 160 | |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 161 | local::stream_protocol::acceptor acceptor(g_io, listenerEp); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file); |
| 163 | |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 164 | 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 Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 170 | |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 171 | BOOST_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 Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 177 | acceptor.close(); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 178 | // the socket file is not removed when the acceptor is closed |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file); |
| 180 | |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 181 | // 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 Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file); |
| 196 | } |
| 197 | |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 198 | BOOST_AUTO_TEST_CASE(NotASocket) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 199 | { |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 200 | auto channel = makeChannel(); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 201 | |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 202 | 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 Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 211 | std::ofstream f(socketPath.string()); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 212 | f.close(); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame] | 213 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::regular_file); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 214 | 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 Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 219 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 220 | |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 221 | BOOST_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 | |
| 237 | BOOST_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 | |
| 276 | BOOST_AUTO_TEST_SUITE_END() // SocketFile |
| 277 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 278 | BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamChannel |
| 279 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 280 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 281 | } // namespace nfd::tests |