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