blob: e7fb76d686e945e077fe32da5602c5112331467f [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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"
Eric Newberry65caf202015-12-17 15:08:16 -070035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Eric Newberry65caf202015-12-17 15:08:16 -070037
Eric Newberry65caf202015-12-17 15:08:16 -070038namespace ip = boost::asio::ip;
39using ip::udp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040using face::UnicastUdpTransport;
Eric Newberry65caf202015-12-17 15:08:16 -070041
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040042class UnicastUdpTransportFixture : public GlobalIoFixture
Eric Newberry65caf202015-12-17 15:08:16 -070043{
44protected:
Eric Newberry65caf202015-12-17 15:08:16 -070045 void
Davide Pesavento22fba352017-10-17 15:53:51 -040046 initialize(ip::address address,
Eric Newberry65caf202015-12-17 15:08:16 -070047 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
48 {
49 udp::socket sock(g_io);
50 sock.connect(udp::endpoint(address, 7070));
51 localEp = sock.local_endpoint();
52
53 remoteConnect(address);
54
Davide Pesaventoa8098582019-03-31 15:48:02 -040055 face = make_unique<Face>(make_unique<DummyLinkService>(),
56 make_unique<UnicastUdpTransport>(std::move(sock), persistency, 3_s));
Eric Newberry65caf202015-12-17 15:08:16 -070057 transport = static_cast<UnicastUdpTransport*>(face->getTransport());
Davide Pesaventoa8098582019-03-31 15:48:02 -040058 receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
Eric Newberry65caf202015-12-17 15:08:16 -070059
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040060 BOOST_REQUIRE_EQUAL(transport->getState(), face::TransportState::UP);
Eric Newberry65caf202015-12-17 15:08:16 -070061 }
62
63 void
64 remoteConnect(ip::address address = ip::address_v4::loopback())
65 {
66 udp::endpoint remoteEp(address, 7070);
67 remoteSocket.open(remoteEp.protocol());
68 remoteSocket.set_option(udp::socket::reuse_address(true));
69 remoteSocket.bind(remoteEp);
70 remoteSocket.connect(localEp);
71 }
72
73 void
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070074 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
75 {
76 remoteSocket.async_receive(boost::asio::buffer(buf),
77 [this, needToCheck] (const boost::system::error_code& error, size_t) {
78 if (needToCheck) {
79 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
80 }
81 limitedIo.afterOp();
82 });
Davide Pesavento14e71f02019-03-28 17:35:25 -040083 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070084 }
85
86 void
Eric Newberry65caf202015-12-17 15:08:16 -070087 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
88 {
89 remoteSocket.async_send(boost::asio::buffer(buf),
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040090 [needToCheck] (const auto& error, size_t) {
Eric Newberry65caf202015-12-17 15:08:16 -070091 if (needToCheck) {
92 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
93 }
94 });
Davide Pesavento14e71f02019-03-28 17:35:25 -040095 limitedIo.defer(1_s);
Eric Newberry65caf202015-12-17 15:08:16 -070096 }
97
98protected:
99 LimitedIo limitedIo;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400100 UnicastUdpTransport* transport = nullptr;
Eric Newberry65caf202015-12-17 15:08:16 -0700101 udp::endpoint localEp;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400102 udp::socket remoteSocket{g_io};
103 std::vector<RxPacket>* receivedPackets = nullptr;
Eric Newberry65caf202015-12-17 15:08:16 -0700104
105private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700106 unique_ptr<Face> face;
Eric Newberry65caf202015-12-17 15:08:16 -0700107};
108
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400109} // namespace nfd::tests
Eric Newberry65caf202015-12-17 15:08:16 -0700110
111#endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP