blob: 6d523ff4155518f0d6bdba02ed3f1cef3fcc42e4 [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"
Junxiao Shicde37ad2015-12-24 01:02:05 -070029#include "tests/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 },
68 [this] (const std::string& reason) {
69 BOOST_FAIL(reason);
70 limitedIo.afterOp();
71 });
72 }
73
74 void
75 connect(local::stream_protocol::socket& client)
76 {
77 client.async_connect(listenerEp,
78 [this] (const boost::system::error_code& error) {
79 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
80 limitedIo.afterOp();
81 });
82 }
83
84protected:
85 LimitedIo limitedIo;
86 unix_stream::Endpoint listenerEp;
87 unique_ptr<UnixStreamChannel> listenerChannel;
88 std::vector<shared_ptr<Face>> listenerFaces;
89};
90
91BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture)
92
93BOOST_AUTO_TEST_CASE(Listen)
94{
95 auto channel = makeChannel();
96 BOOST_CHECK_EQUAL(channel->isListening(), false);
97
98 channel->listen(nullptr, nullptr);
99 BOOST_CHECK_EQUAL(channel->isListening(), true);
100
101 // listen() is idempotent
102 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
103 BOOST_CHECK_EQUAL(channel->isListening(), true);
104}
105
106BOOST_AUTO_TEST_CASE(MultipleAccepts)
107{
108 this->listen();
109
110 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
111 BOOST_CHECK_EQUAL(listenerFaces.size(), 0);
112
113 local::stream_protocol::socket client1(g_io);
114 this->connect(client1);
115
116 BOOST_CHECK_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS);
117 BOOST_CHECK_EQUAL(listenerFaces.size(), 1);
118
119 local::stream_protocol::socket client2(g_io);
120 local::stream_protocol::socket client3(g_io);
121 this->connect(client2);
122 this->connect(client3);
123
124 BOOST_CHECK_EQUAL(limitedIo.run(4, time::seconds(1)), LimitedIo::EXCEED_OPS);
125 BOOST_CHECK_EQUAL(listenerFaces.size(), 3);
126
127 // check face persistency
128 for (const auto& face : listenerFaces) {
129 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
130 }
131}
132
133BOOST_AUTO_TEST_CASE(SocketFile)
134{
135 fs::path socketPath(listenerEp.path());
136 fs::remove(socketPath);
137
138 auto channel = makeChannel();
139 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found);
140
141 channel->listen(nullptr, nullptr);
142 auto status = fs::symlink_status(socketPath);
143 BOOST_CHECK_EQUAL(status.type(), fs::socket_file);
144 BOOST_CHECK_EQUAL(status.permissions(), fs::owner_read | fs::group_read | fs::others_read |
145 fs::owner_write | fs::group_write | fs::others_write);
146
147 channel.reset();
148 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found);
149}
150
151BOOST_AUTO_TEST_CASE(ExistingStaleSocketFile)
152{
153 fs::path socketPath(listenerEp.path());
154 fs::remove(socketPath);
155
156 local::stream_protocol::acceptor acceptor(g_io, listenerEp);
157 acceptor.close();
158 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
159
160 auto channel = makeChannel();
161 BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr));
162 BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file);
163}
164
165BOOST_AUTO_TEST_CASE(ExistingRegularFile)
166{
167 fs::path socketPath(listenerEp.path());
168 fs::remove(socketPath);
169
170 std::ofstream f(listenerEp.path());
171 f.close();
172
173 auto channel = makeChannel();
174 BOOST_CHECK_THROW(channel->listen(nullptr, nullptr), UnixStreamChannel::Error);
175
176 fs::remove(socketPath);
177}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700178
179BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamChannel
180BOOST_AUTO_TEST_SUITE_END() // Face
181
182} // namespace tests
Eric Newberrya98bf932015-09-21 00:58:47 -0700183} // namespace nfd