blob: 77208e5f0fa28310cbb9ab7bbd75df82486d0ee1 [file] [log] [blame]
Eric Newberryf68f3782015-12-11 00:31:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
Davide Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, Regents of the University of California,
Eric Newberryf68f3782015-12-11 00:31:32 -07004 * 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
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040032#include "tests/test-common.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060033#include "tests/daemon/limited-io.hpp"
Davide Pesaventoa8098582019-03-31 15:48:02 -040034#include "tests/daemon/face/dummy-link-service.hpp"
Eric Newberryf68f3782015-12-11 00:31:32 -070035
36namespace nfd {
37namespace face {
38namespace tests {
39
40using namespace nfd::tests;
41namespace ip = boost::asio::ip;
42using ip::tcp;
43
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040044class TcpTransportFixture : public GlobalIoFixture
Eric Newberryf68f3782015-12-11 00:31:32 -070045{
46protected:
47 TcpTransportFixture()
48 : transport(nullptr)
49 , remoteSocket(g_io)
50 , receivedPackets(nullptr)
51 , acceptor(g_io)
52 {
53 }
54
55 void
Weiwei Liudcdf6212016-08-31 14:34:22 -070056 startAccept(const tcp::endpoint& remoteEp)
Eric Newberryf68f3782015-12-11 00:31:32 -070057 {
Weiwei Liudcdf6212016-08-31 14:34:22 -070058 BOOST_REQUIRE(!acceptor.is_open());
Eric Newberryf68f3782015-12-11 00:31:32 -070059 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 });
Weiwei Liudcdf6212016-08-31 14:34:22 -070067 }
68
69 void
70 stopAccept()
71 {
72 BOOST_REQUIRE(acceptor.is_open());
73 acceptor.close();
74 }
75
76 void
Davide Pesavento22fba352017-10-17 15:53:51 -040077 initialize(ip::address address,
Weiwei Liudcdf6212016-08-31 14:34:22 -070078 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
79 {
80 tcp::endpoint remoteEp(address, 7070);
81 startAccept(remoteEp);
Eric Newberryf68f3782015-12-11 00:31:32 -070082
83 tcp::socket sock(g_io);
84 sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) {
85 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
86 limitedIo.afterOp();
87 });
88
Davide Pesavento14e71f02019-03-28 17:35:25 -040089 BOOST_REQUIRE_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -070090
91 localEp = sock.local_endpoint();
Alexander Afanasyevded17422018-04-03 19:00:23 -040092
93 ndn::nfd::FaceScope scope;
94 if (sock.local_endpoint().address().is_loopback() && sock.remote_endpoint().address().is_loopback()) {
95 scope = ndn::nfd::FACE_SCOPE_LOCAL;
96 }
97 else {
98 scope = ndn::nfd::FACE_SCOPE_NON_LOCAL;
99 }
100
Davide Pesaventoa8098582019-03-31 15:48:02 -0400101 face = make_unique<Face>(make_unique<DummyLinkService>(),
Alexander Afanasyevded17422018-04-03 19:00:23 -0400102 make_unique<TcpTransport>(std::move(sock), persistency, scope));
Eric Newberryf68f3782015-12-11 00:31:32 -0700103 transport = static_cast<TcpTransport*>(face->getTransport());
Davide Pesaventoa8098582019-03-31 15:48:02 -0400104 receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
Eric Newberryf68f3782015-12-11 00:31:32 -0700105
106 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
107 }
108
109 void
Eric Newberry65caf202015-12-17 15:08:16 -0700110 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
Eric Newberryf68f3782015-12-11 00:31:32 -0700111 {
112 boost::asio::async_write(remoteSocket, boost::asio::buffer(buf),
113 [needToCheck] (const boost::system::error_code& error, size_t) {
114 if (needToCheck) {
115 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
116 }
117 });
Davide Pesavento14e71f02019-03-28 17:35:25 -0400118 limitedIo.defer(1_s);
Eric Newberryf68f3782015-12-11 00:31:32 -0700119 }
120
121protected:
122 LimitedIo limitedIo;
123 TcpTransport* transport;
124 tcp::endpoint localEp;
125 tcp::socket remoteSocket;
126 std::vector<Transport::Packet>* receivedPackets;
127
128private:
129 tcp::acceptor acceptor;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700130 unique_ptr<Face> face;
Eric Newberryf68f3782015-12-11 00:31:32 -0700131};
132
133} // namespace tests
134} // namespace face
135} // namespace nfd
136
137#endif // NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP