Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 34 | namespace tests { |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 35 | |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 36 | namespace fs = boost::filesystem; |
| 37 | namespace local = boost::asio::local; |
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 | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 44 | listenerEp = unix_stream::Endpoint("nfd-test-unix-stream-channel.sock"); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 47 | virtual unique_ptr<UnixStreamChannel> |
| 48 | makeChannel() final |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 49 | { |
| 50 | return make_unique<UnixStreamChannel>(listenerEp); |
| 51 | } |
| 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 Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 64 | ChannelFixture::unexpectedFailure); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 68 | clientConnect(local::stream_protocol::socket& client) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 69 | { |
| 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 Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 78 | BOOST_AUTO_TEST_SUITE(Face) |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 79 | BOOST_FIXTURE_TEST_SUITE(TestUnixStreamChannel, UnixStreamChannelFixture) |
| 80 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 81 | BOOST_AUTO_TEST_CASE(Uri) |
| 82 | { |
| 83 | auto channel = makeChannel(); |
| 84 | BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(listenerEp)); |
| 85 | } |
| 86 | |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 87 | BOOST_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 | |
| 100 | BOOST_AUTO_TEST_CASE(MultipleAccepts) |
| 101 | { |
| 102 | this->listen(); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(listenerChannel->isListening(), true); |
| 105 | BOOST_CHECK_EQUAL(listenerFaces.size(), 0); |
| 106 | |
| 107 | local::stream_protocol::socket client1(g_io); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 108 | this->clientConnect(client1); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 109 | |
| 110 | BOOST_CHECK_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 111 | BOOST_CHECK_EQUAL(listenerFaces.size(), 1); |
| 112 | |
| 113 | local::stream_protocol::socket client2(g_io); |
| 114 | local::stream_protocol::socket client3(g_io); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 115 | this->clientConnect(client2); |
| 116 | this->clientConnect(client3); |
Spencer Lee | 75142a1 | 2016-04-13 16:55:10 -0700 | [diff] [blame] | 117 | |
| 118 | BOOST_CHECK_EQUAL(limitedIo.run(4, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 119 | BOOST_CHECK_EQUAL(listenerFaces.size(), 3); |
| 120 | |
| 121 | // check face persistency |
| 122 | for (const auto& face : listenerFaces) { |
| 123 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(SocketFile) |
| 128 | { |
| 129 | fs::path socketPath(listenerEp.path()); |
| 130 | fs::remove(socketPath); |
| 131 | |
| 132 | auto channel = makeChannel(); |
| 133 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found); |
| 134 | |
| 135 | channel->listen(nullptr, nullptr); |
| 136 | auto status = fs::symlink_status(socketPath); |
| 137 | BOOST_CHECK_EQUAL(status.type(), fs::socket_file); |
| 138 | BOOST_CHECK_EQUAL(status.permissions(), fs::owner_read | fs::group_read | fs::others_read | |
| 139 | fs::owner_write | fs::group_write | fs::others_write); |
| 140 | |
| 141 | channel.reset(); |
| 142 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::file_not_found); |
| 143 | } |
| 144 | |
| 145 | BOOST_AUTO_TEST_CASE(ExistingStaleSocketFile) |
| 146 | { |
| 147 | fs::path socketPath(listenerEp.path()); |
| 148 | fs::remove(socketPath); |
| 149 | |
| 150 | local::stream_protocol::acceptor acceptor(g_io, listenerEp); |
| 151 | acceptor.close(); |
| 152 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file); |
| 153 | |
| 154 | auto channel = makeChannel(); |
| 155 | BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr)); |
| 156 | BOOST_CHECK_EQUAL(fs::symlink_status(socketPath).type(), fs::socket_file); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_CASE(ExistingRegularFile) |
| 160 | { |
| 161 | fs::path socketPath(listenerEp.path()); |
| 162 | fs::remove(socketPath); |
| 163 | |
| 164 | std::ofstream f(listenerEp.path()); |
| 165 | f.close(); |
| 166 | |
| 167 | auto channel = makeChannel(); |
| 168 | BOOST_CHECK_THROW(channel->listen(nullptr, nullptr), UnixStreamChannel::Error); |
| 169 | |
| 170 | fs::remove(socketPath); |
| 171 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 172 | |
| 173 | BOOST_AUTO_TEST_SUITE_END() // TestUnixStreamChannel |
| 174 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 175 | |
| 176 | } // namespace tests |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 177 | } // namespace nfd |