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