blob: 688028e8b0191f27ad75d38508bb73af638b7d41 [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 Pesaventoc0df94e2023-10-08 19:26:55 -04003 * Copyright (c) 2014-2023, 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 {
Davide Pesaventoef6a5282023-11-03 16:50:10 -040049 BOOST_REQUIRE(!m_acceptor.is_open());
50 m_acceptor.open(remoteEp.protocol());
51 m_acceptor.set_option(boost::asio::socket_base::reuse_address(true));
52 m_acceptor.bind(remoteEp);
53 m_acceptor.listen(1);
54 m_acceptor.async_accept(remoteSocket, [this] (const auto& error) {
Eric Newberryf68f3782015-12-11 00:31:32 -070055 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 {
Davide Pesaventoef6a5282023-11-03 16:50:10 -040063 BOOST_REQUIRE(m_acceptor.is_open());
64 m_acceptor.close();
Weiwei Liudcdf6212016-08-31 14:34:22 -070065 }
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);
Davide Pesaventoef6a5282023-11-03 16:50:10 -040075 sock.async_connect(remoteEp, [this] (const auto& error) {
Eric Newberryf68f3782015-12-11 00:31:32 -070076 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 Pesaventoef6a5282023-11-03 16:50:10 -040092 m_face = make_unique<Face>(make_unique<DummyLinkService>(),
93 make_unique<TcpTransport>(std::move(sock), persistency, scope));
94 transport = static_cast<TcpTransport*>(m_face->getTransport());
95 receivedPackets = &static_cast<DummyLinkService*>(m_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 Pesaventoef6a5282023-11-03 16:50:10 -0400120 tcp::acceptor m_acceptor{g_io};
121 unique_ptr<Face> m_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