blob: 1d1d5ac937206b383c7b35fb93a207532568e2cd [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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"
Davide Pesavento279af1c2022-08-29 20:18:32 -040035#include "tests/daemon/face/transport-test-common.hpp"
Eric Newberryf68f3782015-12-11 00:31:32 -070036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::tests {
Eric Newberryf68f3782015-12-11 00:31:32 -070038
Eric Newberryf68f3782015-12-11 00:31:32 -070039namespace ip = boost::asio::ip;
40using ip::tcp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040041using face::TcpTransport;
Eric Newberryf68f3782015-12-11 00:31:32 -070042
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040043class TcpTransportFixture : public GlobalIoFixture
Eric Newberryf68f3782015-12-11 00:31:32 -070044{
45protected:
Eric Newberryf68f3782015-12-11 00:31:32 -070046 void
Weiwei Liudcdf6212016-08-31 14:34:22 -070047 startAccept(const tcp::endpoint& remoteEp)
Eric Newberryf68f3782015-12-11 00:31:32 -070048 {
Weiwei Liudcdf6212016-08-31 14:34:22 -070049 BOOST_REQUIRE(!acceptor.is_open());
Eric Newberryf68f3782015-12-11 00:31:32 -070050 acceptor.open(remoteEp.protocol());
51 acceptor.set_option(tcp::acceptor::reuse_address(true));
52 acceptor.bind(remoteEp);
53 acceptor.listen(1);
54 acceptor.async_accept(remoteSocket, [this] (const boost::system::error_code& error) {
55 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
56 limitedIo.afterOp();
57 });
Weiwei Liudcdf6212016-08-31 14:34:22 -070058 }
59
60 void
61 stopAccept()
62 {
63 BOOST_REQUIRE(acceptor.is_open());
64 acceptor.close();
65 }
66
67 void
Davide Pesavento279af1c2022-08-29 20:18:32 -040068 initialize(const shared_ptr<const ndn::net::NetworkInterface>&, const ip::address& address,
Weiwei Liudcdf6212016-08-31 14:34:22 -070069 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
70 {
71 tcp::endpoint remoteEp(address, 7070);
72 startAccept(remoteEp);
Eric Newberryf68f3782015-12-11 00:31:32 -070073
74 tcp::socket sock(g_io);
75 sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) {
76 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
77 limitedIo.afterOp();
78 });
79
Davide Pesavento14e71f02019-03-28 17:35:25 -040080 BOOST_REQUIRE_EQUAL(limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberryf68f3782015-12-11 00:31:32 -070081
82 localEp = sock.local_endpoint();
Alexander Afanasyevded17422018-04-03 19:00:23 -040083
84 ndn::nfd::FaceScope scope;
85 if (sock.local_endpoint().address().is_loopback() && sock.remote_endpoint().address().is_loopback()) {
86 scope = ndn::nfd::FACE_SCOPE_LOCAL;
87 }
88 else {
89 scope = ndn::nfd::FACE_SCOPE_NON_LOCAL;
90 }
91
Davide Pesaventoa8098582019-03-31 15:48:02 -040092 face = make_unique<Face>(make_unique<DummyLinkService>(),
Alexander Afanasyevded17422018-04-03 19:00:23 -040093 make_unique<TcpTransport>(std::move(sock), persistency, scope));
Eric Newberryf68f3782015-12-11 00:31:32 -070094 transport = static_cast<TcpTransport*>(face->getTransport());
Davide Pesaventoa8098582019-03-31 15:48:02 -040095 receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
Eric Newberryf68f3782015-12-11 00:31:32 -070096
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040097 BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
Eric Newberryf68f3782015-12-11 00:31:32 -070098 }
99
100 void
Eric Newberry65caf202015-12-17 15:08:16 -0700101 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
Eric Newberryf68f3782015-12-11 00:31:32 -0700102 {
103 boost::asio::async_write(remoteSocket, boost::asio::buffer(buf),
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400104 [needToCheck] (const auto& error, size_t) {
Eric Newberryf68f3782015-12-11 00:31:32 -0700105 if (needToCheck) {
106 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
107 }
108 });
Davide Pesavento14e71f02019-03-28 17:35:25 -0400109 limitedIo.defer(1_s);
Eric Newberryf68f3782015-12-11 00:31:32 -0700110 }
111
112protected:
113 LimitedIo limitedIo;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400114 TcpTransport* transport = nullptr;
Eric Newberryf68f3782015-12-11 00:31:32 -0700115 tcp::endpoint localEp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400116 tcp::socket remoteSocket{g_io};
117 std::vector<RxPacket>* receivedPackets = nullptr;
Eric Newberryf68f3782015-12-11 00:31:32 -0700118
119private:
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400120 tcp::acceptor acceptor{g_io};
Junxiao Shicde37ad2015-12-24 01:02:05 -0700121 unique_ptr<Face> face;
Eric Newberryf68f3782015-12-11 00:31:32 -0700122};
123
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400124} // namespace nfd::tests
Eric Newberryf68f3782015-12-11 00:31:32 -0700125
126#endif // NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP