blob: 97b195fd9c8b419c07e2421085613bad17a4879b [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"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "face/face.hpp"
Eric Newberryf68f3782015-12-11 00:31:32 -070031
32#include "dummy-receive-link-service.hpp"
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020033#include "test-ip.hpp"
Eric Newberryf68f3782015-12-11 00:31:32 -070034#include "tests/limited-io.hpp"
35
36namespace nfd {
37namespace face {
38namespace tests {
39
40using namespace nfd::tests;
41namespace ip = boost::asio::ip;
42using ip::tcp;
43
44class TcpTransportFixture : public BaseFixture
45{
46protected:
47 TcpTransportFixture()
48 : transport(nullptr)
49 , remoteSocket(g_io)
50 , receivedPackets(nullptr)
51 , acceptor(g_io)
52 {
53 }
54
55 void
56 initialize(ip::address address = ip::address_v4::loopback())
57 {
58 tcp::endpoint remoteEp(address, 7070);
59 acceptor.open(remoteEp.protocol());
60 acceptor.set_option(tcp::acceptor::reuse_address(true));
61 acceptor.bind(remoteEp);
62 acceptor.listen(1);
63 acceptor.async_accept(remoteSocket, [this] (const boost::system::error_code& error) {
64 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
65 limitedIo.afterOp();
66 });
67
68 tcp::socket sock(g_io);
69 sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) {
70 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
71 limitedIo.afterOp();
72 });
73
74 BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS);
75
76 localEp = sock.local_endpoint();
Junxiao Shicde37ad2015-12-24 01:02:05 -070077 face = make_unique<Face>(make_unique<DummyReceiveLinkService>(),
78 make_unique<TcpTransport>(std::move(sock),
79 ndn::nfd::FACE_PERSISTENCY_PERSISTENT));
Eric Newberryf68f3782015-12-11 00:31:32 -070080 transport = static_cast<TcpTransport*>(face->getTransport());
81 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
82
83 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
84 }
85
86 void
Eric Newberry65caf202015-12-17 15:08:16 -070087 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
Eric Newberryf68f3782015-12-11 00:31:32 -070088 {
89 boost::asio::async_write(remoteSocket, boost::asio::buffer(buf),
90 [needToCheck] (const boost::system::error_code& error, size_t) {
91 if (needToCheck) {
92 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
93 }
94 });
Eric Newberry65caf202015-12-17 15:08:16 -070095 limitedIo.defer(time::seconds(1));
Eric Newberryf68f3782015-12-11 00:31:32 -070096 }
97
98protected:
99 LimitedIo limitedIo;
100 TcpTransport* transport;
101 tcp::endpoint localEp;
102 tcp::socket remoteSocket;
103 std::vector<Transport::Packet>* receivedPackets;
104
105private:
106 tcp::acceptor acceptor;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700107 unique_ptr<Face> face;
Eric Newberryf68f3782015-12-11 00:31:32 -0700108};
109
110} // namespace tests
111} // namespace face
112} // namespace nfd
113
114#endif // NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP