Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -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 | |
| 26 | #ifndef NFD_TESTS_DAEMON_FACE_UNIX_STREAM_TRANSPORT_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_UNIX_STREAM_TRANSPORT_FIXTURE_HPP |
| 28 | |
| 29 | #include "face/unix-stream-transport.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | #include "face/face.hpp" |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame^] | 32 | #include "tests/test-common.hpp" |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 33 | #include "tests/daemon/limited-io.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame^] | 34 | #include "dummy-receive-link-service.hpp" |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 35 | |
| 36 | #include <boost/filesystem.hpp> |
| 37 | |
| 38 | namespace nfd { |
| 39 | namespace face { |
| 40 | namespace tests { |
| 41 | |
| 42 | using namespace nfd::tests; |
| 43 | typedef boost::asio::local::stream_protocol unix_stream; |
| 44 | |
| 45 | /** \brief automatically unlinks the socket file of a Unix stream acceptor |
| 46 | */ |
| 47 | class AcceptorWithCleanup : public unix_stream::acceptor |
| 48 | { |
| 49 | public: |
| 50 | explicit |
| 51 | AcceptorWithCleanup(boost::asio::io_service& io, const std::string& path = "") |
| 52 | : unix_stream::acceptor(io) |
| 53 | { |
| 54 | this->open(); |
| 55 | |
| 56 | if (path.empty()) { |
| 57 | this->bind("nfd-unix-stream-test." + to_string(time::system_clock::now().time_since_epoch().count()) + ".sock"); |
| 58 | } |
| 59 | else { |
| 60 | this->bind(path); |
| 61 | } |
| 62 | |
| 63 | this->listen(1); |
| 64 | } |
| 65 | |
| 66 | ~AcceptorWithCleanup() |
| 67 | { |
| 68 | boost::system::error_code ec; |
| 69 | |
| 70 | std::string path = this->local_endpoint(ec).path(); |
| 71 | if (ec) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | this->close(ec); |
| 76 | boost::filesystem::remove(path, ec); |
| 77 | } |
| 78 | }; |
| 79 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame^] | 80 | class UnixStreamTransportFixture : public GlobalIoFixture |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 81 | { |
| 82 | protected: |
| 83 | UnixStreamTransportFixture() |
| 84 | : transport(nullptr) |
| 85 | , remoteSocket(g_io) |
| 86 | , receivedPackets(nullptr) |
| 87 | , acceptor(g_io) |
| 88 | { |
| 89 | } |
| 90 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 91 | std::pair<bool, std::string> |
| 92 | checkPreconditions() const |
| 93 | { |
| 94 | return {true, ""}; |
| 95 | } |
| 96 | |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 97 | void |
| 98 | initialize() |
| 99 | { |
| 100 | unix_stream::socket sock(g_io); |
| 101 | acceptor.async_accept(sock, [this] (const boost::system::error_code& error) { |
| 102 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 103 | limitedIo.afterOp(); |
| 104 | }); |
| 105 | |
| 106 | unix_stream::endpoint remoteEp(acceptor.local_endpoint()); |
| 107 | remoteSocket.async_connect(remoteEp, [this] (const boost::system::error_code& error) { |
| 108 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 109 | limitedIo.afterOp(); |
| 110 | }); |
| 111 | |
| 112 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 113 | |
| 114 | localEp = sock.local_endpoint(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 115 | face = make_unique<Face>(make_unique<DummyReceiveLinkService>(), |
| 116 | make_unique<UnixStreamTransport>(std::move(sock))); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 117 | transport = static_cast<UnixStreamTransport*>(face->getTransport()); |
| 118 | receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets; |
| 119 | |
| 120 | BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | remoteWrite(const ndn::Buffer& buf, bool needToCheck = true) |
| 125 | { |
| 126 | boost::asio::async_write(remoteSocket, boost::asio::buffer(buf), |
| 127 | [needToCheck] (const boost::system::error_code& error, size_t) { |
| 128 | if (needToCheck) { |
| 129 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 130 | } |
| 131 | }); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 132 | limitedIo.defer(time::seconds(1)); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | protected: |
| 136 | LimitedIo limitedIo; |
| 137 | UnixStreamTransport* transport; |
| 138 | unix_stream::endpoint localEp; |
| 139 | unix_stream::socket remoteSocket; |
| 140 | std::vector<Transport::Packet>* receivedPackets; |
| 141 | |
| 142 | private: |
| 143 | AcceptorWithCleanup acceptor; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 144 | unique_ptr<Face> face; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace tests |
| 148 | } // namespace face |
| 149 | } // namespace nfd |
| 150 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 151 | #endif // NFD_TESTS_DAEMON_FACE_UNIX_STREAM_TRANSPORT_FIXTURE_HPP |