blob: e4ce0cfa1e6bc1360adca47e884d03cfef148bf1 [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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 Pesavento9a00fab2016-09-27 11:22:46 +020044 listenerEp = unix_stream::Endpoint("nfd-test-unix-stream-channel.sock");
Spencer Lee75142a12016-04-13 16:55:10 -070045 }
46
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040047 shared_ptr<UnixStreamChannel>
Davide Pesavento9a00fab2016-09-27 11:22:46 +020048 makeChannel() final
Spencer Lee75142a12016-04-13 16:55:10 -070049 {
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040050 return std::make_shared<UnixStreamChannel>(listenerEp, false);
Spencer Lee75142a12016-04-13 16:55:10 -070051 }
52
53 void
54 listen()
55 {
56 listenerChannel = makeChannel();
57 listenerChannel->listen(
58 [this] (const shared_ptr<Face>& newFace) {
59 BOOST_REQUIRE(newFace != nullptr);
60 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
61 listenerFaces.push_back(newFace);
62 limitedIo.afterOp();
63 },
Davide Pesavento9a00fab2016-09-27 11:22:46 +020064 ChannelFixture::unexpectedFailure);
Spencer Lee75142a12016-04-13 16:55:10 -070065 }
66
67 void
Davide Pesavento9a00fab2016-09-27 11:22:46 +020068 clientConnect(local::stream_protocol::socket& client)
Spencer Lee75142a12016-04-13 16:55:10 -070069 {
70 client.async_connect(listenerEp,
71 [this] (const boost::system::error_code& error) {
72 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
73 limitedIo.afterOp();
74 });
75 }
Spencer Lee75142a12016-04-13 16:55:10 -070076};
77
Davide Pesavento9a00fab2016-09-27 11:22:46 +020078BOOST_AUTO_TEST_SUITE(Face)
Spencer Lee75142a12016-04-13 16:55:10 -070079BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture)
80
Davide Pesavento9a00fab2016-09-27 11:22:46 +020081BOOST_AUTO_TEST_CASE(Uri)
82{
83 auto channel = makeChannel();
84 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(listenerEp));
85}
86
Spencer Lee75142a12016-04-13 16:55:10 -070087BOOST_AUTO_TEST_CASE(Listen)
88{
89 auto channel = makeChannel();
90 BOOST_CHECK_EQUAL(channel->isListening(), false);
91
92 channel->listen(nullptr, nullptr);
93 BOOST_CHECK_EQUAL(channel->isListening(), true);
94
95 // listen() is idempotent
96 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
97 BOOST_CHECK_EQUAL(channel->isListening(), true);
98}
99
100BOOST_AUTO_TEST_CASE(MultipleAccepts)
101{
102 this->listen();
103
104 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400105 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
Spencer Lee75142a12016-04-13 16:55:10 -0700106
107 local::stream_protocol::socket client1(g_io);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200108 this->clientConnect(client1);
Spencer Lee75142a12016-04-13 16:55:10 -0700109
Davide Pesavento14e71f02019-03-28 17:35:25 -0400110 BOOST_CHECK_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400111 BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
Spencer Lee75142a12016-04-13 16:55:10 -0700112
113 local::stream_protocol::socket client2(g_io);
114 local::stream_protocol::socket client3(g_io);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200115 this->clientConnect(client2);
116 this->clientConnect(client3);
Spencer Lee75142a12016-04-13 16:55:10 -0700117
Davide Pesavento14e71f02019-03-28 17:35:25 -0400118 BOOST_CHECK_EQUAL(limitedIo.run(4, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400119 BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
Spencer Lee75142a12016-04-13 16:55:10 -0700120 BOOST_CHECK_EQUAL(listenerFaces.size(), 3);
121
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400122 // check face persistency and channel association
Spencer Lee75142a12016-04-13 16:55:10 -0700123 for (const auto& face : listenerFaces) {
124 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400125 BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel);
Spencer Lee75142a12016-04-13 16:55:10 -0700126 }
127}
128
129BOOST_AUTO_TEST_CASE(SocketFile)
130{
131 fs::path socketPath(listenerEp.path());
132 fs::remove(socketPath);
133
134 auto channel = makeChannel();
135 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found);
136
137 channel->listen(nullptr, nullptr);
138 auto status = fs::symlink_status(socketPath);
139 BOOST_CHECK_EQUAL(status.type(), fs::socket_file);
140 BOOST_CHECK_EQUAL(status.permissions(), fs::owner_read | fs::group_read | fs::others_read |
141 fs::owner_write | fs::group_write | fs::others_write);
142
143 channel.reset();
144 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found);
145}
146
147BOOST_AUTO_TEST_CASE(ExistingStaleSocketFile)
148{
149 fs::path socketPath(listenerEp.path());
150 fs::remove(socketPath);
151
152 local::stream_protocol::acceptor acceptor(g_io, listenerEp);
153 acceptor.close();
154 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
155
156 auto channel = makeChannel();
157 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
158 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
159}
160
161BOOST_AUTO_TEST_CASE(ExistingRegularFile)
162{
163 fs::path socketPath(listenerEp.path());
164 fs::remove(socketPath);
165
166 std::ofstream f(listenerEp.path());
167 f.close();
168
169 auto channel = makeChannel();
170 BOOST_CHECK_THROW(channel->listen(nullptr, nullptr), UnixStreamChannel::Error);
171
172 fs::remove(socketPath);
173}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700174
175BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamChannel
176BOOST_AUTO_TEST_SUITE_END() // Face
177
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400178} // namespace nfd::tests