blob: 03594a1eaa66e61c5a3c00cbd3a0085b99a33eb7 [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_TCP_TRANSPORT_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP
28
29#include "face/tcp-transport.hpp"
30#include "face/lp-face.hpp"
31
32#include "dummy-receive-link-service.hpp"
33#include "get-available-interface-ip.hpp"
34#include "transport-test-common.hpp"
35
36#include "tests/limited-io.hpp"
37
38namespace nfd {
39namespace face {
40namespace tests {
41
42using namespace nfd::tests;
43namespace ip = boost::asio::ip;
44using ip::tcp;
45
46class TcpTransportFixture : public BaseFixture
47{
48protected:
49 TcpTransportFixture()
50 : transport(nullptr)
51 , remoteSocket(g_io)
52 , receivedPackets(nullptr)
53 , acceptor(g_io)
54 {
55 }
56
57 void
58 initialize(ip::address address = ip::address_v4::loopback())
59 {
60 tcp::endpoint remoteEp(address, 7070);
61 acceptor.open(remoteEp.protocol());
62 acceptor.set_option(tcp::acceptor::reuse_address(true));
63 acceptor.bind(remoteEp);
64 acceptor.listen(1);
65 acceptor.async_accept(remoteSocket, [this] (const boost::system::error_code& error) {
66 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
67 limitedIo.afterOp();
68 });
69
70 tcp::socket sock(g_io);
71 sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) {
72 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
73 limitedIo.afterOp();
74 });
75
76 BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS);
77
78 localEp = sock.local_endpoint();
79 face = make_unique<LpFace>(make_unique<DummyReceiveLinkService>(),
80 make_unique<TcpTransport>(std::move(sock),
81 ndn::nfd::FACE_PERSISTENCY_PERSISTENT));
82 transport = static_cast<TcpTransport*>(face->getTransport());
83 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
84
85 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
86 }
87
88 void
89 remoteWrite(const ndn::Buffer& buf, bool needToCheck = true)
90 {
91 boost::asio::async_write(remoteSocket, boost::asio::buffer(buf),
92 [needToCheck] (const boost::system::error_code& error, size_t) {
93 if (needToCheck) {
94 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
95 }
96 });
97 limitedIo.defer(time::milliseconds(50));
98 }
99
100protected:
101 LimitedIo limitedIo;
102 TcpTransport* transport;
103 tcp::endpoint localEp;
104 tcp::socket remoteSocket;
105 std::vector<Transport::Packet>* receivedPackets;
106
107private:
108 tcp::acceptor acceptor;
109 unique_ptr<LpFace> face;
110};
111
112} // namespace tests
113} // namespace face
114} // namespace nfd
115
116#endif // NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP