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()); |
| 45 | fs::remove_all(socketPath.parent_path()); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 48 | shared_ptr<UnixStreamChannel> |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 49 | makeChannel() final |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 50 | { |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 51 | return std::make_shared<UnixStreamChannel>(listenerEp, false); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 52 | } |
| 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 Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 65 | ChannelFixture::unexpectedFailure); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 69 | clientConnect(local::stream_protocol::socket& client) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 70 | { |
| 71 | client.async_connect(listenerEp, |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame^] | 72 | [this] (const auto& error) { |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 73 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 74 | limitedIo.afterOp(); |
| 75 | }); |
| 76 | } |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame^] | 77 | |
| 78 | protected: |
| 79 | fs::path socketPath = fs::path(UNIT_TESTS_TMPDIR) / "unix-stream-channel" / "test" / "foo.sock"; |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 82 | BOOST_AUTO_TEST_SUITE(Face) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 83 | BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture) |
| 84 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 85 | BOOST_AUTO_TEST_CASE(Uri) |
| 86 | { |
| 87 | auto channel = makeChannel(); |
| 88 | BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(listenerEp)); |
| 89 | } |
| 90 | |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 91 | BOOST_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 | |
| 104 | BOOST_AUTO_TEST_CASE(MultipleAccepts) |
| 105 | { |
| 106 | this->listen(); |
| 107 | |
| 108 | BOOST_CHECK_EQUAL(listenerChannel->isListening(), true); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 110 | |
| 111 | local::stream_protocol::socket client1(g_io); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 112 | this->clientConnect(client1); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 113 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 116 | |
| 117 | local::stream_protocol::socket client2(g_io); |
| 118 | local::stream_protocol::socket client3(g_io); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 119 | this->clientConnect(client2); |
| 120 | this->clientConnect(client3); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 121 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(limitedIo.run(4, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | c19408d | 2017-04-08 00:42:55 -0400 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(listenerFaces.size(), 3); |
| 125 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 126 | // check face persistency and channel association |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 127 | for (const auto& face : listenerFaces) { |
| 128 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(SocketFile) |
| 134 | { |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 135 | 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 Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame^] | 148 | BOOST_AUTO_TEST_CASE(ExistingSocketFile) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 149 | { |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame^] | 150 | fs::create_directories(socketPath.parent_path()); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 151 | local::stream_protocol::acceptor acceptor(g_io, listenerEp); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 152 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file); |
| 153 | |
| 154 | auto channel = makeChannel(); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame^] | 155 | 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 Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 160 | BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr)); |
| 161 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file); |
| 162 | } |
| 163 | |
| 164 | BOOST_AUTO_TEST_CASE(ExistingRegularFile) |
| 165 | { |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame^] | 166 | auto guard = ndn::make_scope_exit([=] { fs::remove(socketPath); }); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 167 | |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame^] | 168 | fs::create_directories(socketPath.parent_path()); |
| 169 | std::ofstream f(socketPath.string()); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 170 | f.close(); |
Davide Pesavento | 4c95771 | 2024-01-01 15:40:06 -0500 | [diff] [blame^] | 171 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::regular_file); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 172 | |
| 173 | auto channel = makeChannel(); |
| 174 | BOOST_CHECK_THROW(channel->listen(nullptr, nullptr), UnixStreamChannel::Error); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 175 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 176 | |
| 177 | BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamChannel |
| 178 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 179 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 180 | } // namespace nfd::tests |