blob: 24d7e2656a097bca28004754752912214b747180 [file] [log] [blame]
Eric Newberry65caf202015-12-17 15:08:16 -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 Newberry65caf202015-12-17 15:08:16 -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_UNICAST_UDP_TRANSPORT_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP
28
29#include "face/unicast-udp-transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "face/face.hpp"
Eric Newberry65caf202015-12-17 15:08:16 -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 Newberry65caf202015-12-17 15:08:16 -070036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::tests {
Eric Newberry65caf202015-12-17 15:08:16 -070038
Eric Newberry65caf202015-12-17 15:08:16 -070039namespace ip = boost::asio::ip;
40using ip::udp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040041using face::UnicastUdpTransport;
Eric Newberry65caf202015-12-17 15:08:16 -070042
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040043class UnicastUdpTransportFixture : public GlobalIoFixture
Eric Newberry65caf202015-12-17 15:08:16 -070044{
45protected:
Eric Newberry65caf202015-12-17 15:08:16 -070046 void
Davide Pesavento279af1c2022-08-29 20:18:32 -040047 initialize(const shared_ptr<const ndn::net::NetworkInterface>&, const ip::address& address,
Eric Newberry65caf202015-12-17 15:08:16 -070048 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
49 {
50 udp::socket sock(g_io);
51 sock.connect(udp::endpoint(address, 7070));
52 localEp = sock.local_endpoint();
53
54 remoteConnect(address);
55
Davide Pesaventoa8098582019-03-31 15:48:02 -040056 face = make_unique<Face>(make_unique<DummyLinkService>(),
57 make_unique<UnicastUdpTransport>(std::move(sock), persistency, 3_s));
Eric Newberry65caf202015-12-17 15:08:16 -070058 transport = static_cast<UnicastUdpTransport*>(face->getTransport());
Davide Pesaventoa8098582019-03-31 15:48:02 -040059 receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
Eric Newberry65caf202015-12-17 15:08:16 -070060
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040061 BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
Eric Newberry65caf202015-12-17 15:08:16 -070062 }
63
64 void
Davide Pesavento279af1c2022-08-29 20:18:32 -040065 remoteConnect(const ip::address& address = ip::address_v4::loopback())
Eric Newberry65caf202015-12-17 15:08:16 -070066 {
67 udp::endpoint remoteEp(address, 7070);
68 remoteSocket.open(remoteEp.protocol());
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040069 remoteSocket.set_option(boost::asio::socket_base::reuse_address(true));
Eric Newberry65caf202015-12-17 15:08:16 -070070 remoteSocket.bind(remoteEp);
71 remoteSocket.connect(localEp);
72 }
73
74 void
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070075 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
76 {
77 remoteSocket.async_receive(boost::asio::buffer(buf),
78 [this, needToCheck] (const boost::system::error_code& error, size_t) {
79 if (needToCheck) {
80 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
81 }
82 limitedIo.afterOp();
83 });
Davide Pesavento14e71f02019-03-28 17:35:25 -040084 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070085 }
86
87 void
Eric Newberry65caf202015-12-17 15:08:16 -070088 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
89 {
90 remoteSocket.async_send(boost::asio::buffer(buf),
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040091 [needToCheck] (const auto& error, size_t) {
Eric Newberry65caf202015-12-17 15:08:16 -070092 if (needToCheck) {
93 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
94 }
95 });
Davide Pesavento14e71f02019-03-28 17:35:25 -040096 limitedIo.defer(1_s);
Eric Newberry65caf202015-12-17 15:08:16 -070097 }
98
99protected:
100 LimitedIo limitedIo;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400101 UnicastUdpTransport* transport = nullptr;
Eric Newberry65caf202015-12-17 15:08:16 -0700102 udp::endpoint localEp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400103 udp::socket remoteSocket{g_io};
104 std::vector<RxPacket>* receivedPackets = nullptr;
Eric Newberry65caf202015-12-17 15:08:16 -0700105
106private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700107 unique_ptr<Face> face;
Eric Newberry65caf202015-12-17 15:08:16 -0700108};
109
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400110} // namespace nfd::tests
Eric Newberry65caf202015-12-17 15:08:16 -0700111
112#endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP