blob: 37eae6d3bd867f4defb9888ac338cc60178ea9a9 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spencer Lee75142a12016-04-13 16:55:10 -07003 * Copyright (c) 2014-2016, 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
Spencer Lee75142a12016-04-13 16:55:10 -070028#include "tests/limited-io.hpp"
Eric Newberry42602412016-08-27 09:33:18 -070029#include "factory-test-common.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070030
Spencer Lee75142a12016-04-13 16:55:10 -070031#include <boost/filesystem.hpp>
32#include <fstream>
33
Eric Newberrya98bf932015-09-21 00:58:47 -070034namespace nfd {
Junxiao Shicde37ad2015-12-24 01:02:05 -070035namespace tests {
Eric Newberrya98bf932015-09-21 00:58:47 -070036
Junxiao Shicde37ad2015-12-24 01:02:05 -070037BOOST_AUTO_TEST_SUITE(Face)
Eric Newberrya98bf932015-09-21 00:58:47 -070038
Spencer Lee75142a12016-04-13 16:55:10 -070039using nfd::Face;
40namespace fs = boost::filesystem;
41namespace local = boost::asio::local;
Junxiao Shicde37ad2015-12-24 01:02:05 -070042
Spencer Lee75142a12016-04-13 16:55:10 -070043class UnixStreamChannelFixture : public BaseFixture
44{
45protected:
46 UnixStreamChannelFixture()
47 : listenerEp("nfd-test-unix-stream-channel.sock")
48 {
49 }
50
51 unique_ptr<UnixStreamChannel>
52 makeChannel()
53 {
54 return make_unique<UnixStreamChannel>(listenerEp);
55 }
56
57 void
58 listen()
59 {
60 listenerChannel = makeChannel();
61 listenerChannel->listen(
62 [this] (const shared_ptr<Face>& newFace) {
63 BOOST_REQUIRE(newFace != nullptr);
64 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
65 listenerFaces.push_back(newFace);
66 limitedIo.afterOp();
67 },
Eric Newberry42602412016-08-27 09:33:18 -070068 &failIfError);
Spencer Lee75142a12016-04-13 16:55:10 -070069 }
70
71 void
72 connect(local::stream_protocol::socket& client)
73 {
74 client.async_connect(listenerEp,
75 [this] (const boost::system::error_code& error) {
76 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
77 limitedIo.afterOp();
78 });
79 }
80
81protected:
82 LimitedIo limitedIo;
83 unix_stream::Endpoint listenerEp;
84 unique_ptr<UnixStreamChannel> listenerChannel;
85 std::vector<shared_ptr<Face>> listenerFaces;
86};
87
88BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture)
89
90BOOST_AUTO_TEST_CASE(Listen)
91{
92 auto channel = makeChannel();
93 BOOST_CHECK_EQUAL(channel->isListening(), false);
94
95 channel->listen(nullptr, nullptr);
96 BOOST_CHECK_EQUAL(channel->isListening(), true);
97
98 // listen() is idempotent
99 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
100 BOOST_CHECK_EQUAL(channel->isListening(), true);
101}
102
103BOOST_AUTO_TEST_CASE(MultipleAccepts)
104{
105 this->listen();
106
107 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
108 BOOST_CHECK_EQUAL(listenerFaces.size(), 0);
109
110 local::stream_protocol::socket client1(g_io);
111 this->connect(client1);
112
113 BOOST_CHECK_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS);
114 BOOST_CHECK_EQUAL(listenerFaces.size(), 1);
115
116 local::stream_protocol::socket client2(g_io);
117 local::stream_protocol::socket client3(g_io);
118 this->connect(client2);
119 this->connect(client3);
120
121 BOOST_CHECK_EQUAL(limitedIo.run(4, time::seconds(1)), LimitedIo::EXCEED_OPS);
122 BOOST_CHECK_EQUAL(listenerFaces.size(), 3);
123
124 // check face persistency
125 for (const auto& face : listenerFaces) {
126 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
127 }
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
Eric Newberrya98bf932015-09-21 00:58:47 -0700180} // namespace nfd