blob: fa9e48057c8faa57efe5631323333a0e3b53245e [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/*
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -04003 * Copyright (c) 2014-2020, 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
Eric Newberrya98bf932015-09-21 00:58:47 -070033namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040034namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070035namespace tests {
Eric Newberrya98bf932015-09-21 00:58:47 -070036
Spencer Lee75142a12016-04-13 16:55:10 -070037namespace fs = boost::filesystem;
38namespace local = boost::asio::local;
Junxiao Shicde37ad2015-12-24 01:02:05 -070039
Davide Pesavento9a00fab2016-09-27 11:22:46 +020040class UnixStreamChannelFixture : public ChannelFixture<UnixStreamChannel, unix_stream::Endpoint>
Spencer Lee75142a12016-04-13 16:55:10 -070041{
42protected:
43 UnixStreamChannelFixture()
Spencer Lee75142a12016-04-13 16:55:10 -070044 {
Davide Pesavento9a00fab2016-09-27 11:22:46 +020045 listenerEp = unix_stream::Endpoint("nfd-test-unix-stream-channel.sock");
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,
72 [this] (const boost::system::error_code& error) {
73 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
74 limitedIo.afterOp();
75 });
76 }
Spencer Lee75142a12016-04-13 16:55:10 -070077};
78
Davide Pesavento9a00fab2016-09-27 11:22:46 +020079BOOST_AUTO_TEST_SUITE(Face)
Spencer Lee75142a12016-04-13 16:55:10 -070080BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture)
81
Davide Pesavento9a00fab2016-09-27 11:22:46 +020082BOOST_AUTO_TEST_CASE(Uri)
83{
84 auto channel = makeChannel();
85 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(listenerEp));
86}
87
Spencer Lee75142a12016-04-13 16:55:10 -070088BOOST_AUTO_TEST_CASE(Listen)
89{
90 auto channel = makeChannel();
91 BOOST_CHECK_EQUAL(channel->isListening(), false);
92
93 channel->listen(nullptr, nullptr);
94 BOOST_CHECK_EQUAL(channel->isListening(), true);
95
96 // listen() is idempotent
97 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
98 BOOST_CHECK_EQUAL(channel->isListening(), true);
99}
100
101BOOST_AUTO_TEST_CASE(MultipleAccepts)
102{
103 this->listen();
104
105 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400106 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
Spencer Lee75142a12016-04-13 16:55:10 -0700107
108 local::stream_protocol::socket client1(g_io);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200109 this->clientConnect(client1);
Spencer Lee75142a12016-04-13 16:55:10 -0700110
Davide Pesavento14e71f02019-03-28 17:35:25 -0400111 BOOST_CHECK_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400112 BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
Spencer Lee75142a12016-04-13 16:55:10 -0700113
114 local::stream_protocol::socket client2(g_io);
115 local::stream_protocol::socket client3(g_io);
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200116 this->clientConnect(client2);
117 this->clientConnect(client3);
Spencer Lee75142a12016-04-13 16:55:10 -0700118
Davide Pesavento14e71f02019-03-28 17:35:25 -0400119 BOOST_CHECK_EQUAL(limitedIo.run(4, 1_s), LimitedIo::EXCEED_OPS);
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400120 BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
Spencer Lee75142a12016-04-13 16:55:10 -0700121 BOOST_CHECK_EQUAL(listenerFaces.size(), 3);
122
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400123 // check face persistency and channel association
Spencer Lee75142a12016-04-13 16:55:10 -0700124 for (const auto& face : listenerFaces) {
125 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400126 BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel);
Spencer Lee75142a12016-04-13 16:55:10 -0700127 }
128}
129
130BOOST_AUTO_TEST_CASE(SocketFile)
131{
132 fs::path socketPath(listenerEp.path());
133 fs::remove(socketPath);
134
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
148BOOST_AUTO_TEST_CASE(ExistingStaleSocketFile)
149{
150 fs::path socketPath(listenerEp.path());
151 fs::remove(socketPath);
152
153 local::stream_protocol::acceptor acceptor(g_io, listenerEp);
154 acceptor.close();
155 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
156
157 auto channel = makeChannel();
158 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
159 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
160}
161
162BOOST_AUTO_TEST_CASE(ExistingRegularFile)
163{
164 fs::path socketPath(listenerEp.path());
165 fs::remove(socketPath);
166
167 std::ofstream f(listenerEp.path());
168 f.close();
169
170 auto channel = makeChannel();
171 BOOST_CHECK_THROW(channel->listen(nullptr, nullptr), UnixStreamChannel::Error);
172
173 fs::remove(socketPath);
174}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700175
176BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamChannel
177BOOST_AUTO_TEST_SUITE_END() // Face
178
179} // namespace tests
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400180} // namespace face
Eric Newberrya98bf932015-09-21 00:58:47 -0700181} // namespace nfd