blob: ce9006d8333300cf676672f8122e61eb2b1a75d7 [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 Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, 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
36namespace nfd {
37namespace face {
38namespace tests {
39
40using namespace nfd::tests;
41namespace ip = boost::asio::ip;
42using ip::udp;
43
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040044class UnicastUdpTransportFixture : public GlobalIoFixture
Eric Newberry65caf202015-12-17 15:08:16 -070045{
46protected:
47 UnicastUdpTransportFixture()
48 : transport(nullptr)
49 , remoteSocket(g_io)
50 , receivedPackets(nullptr)
51 {
52 }
53
54 void
Davide Pesavento22fba352017-10-17 15:53:51 -040055 initialize(ip::address address,
Eric Newberry65caf202015-12-17 15:08:16 -070056 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
57 {
58 udp::socket sock(g_io);
59 sock.connect(udp::endpoint(address, 7070));
60 localEp = sock.local_endpoint();
61
62 remoteConnect(address);
63
Davide Pesaventoa8098582019-03-31 15:48:02 -040064 face = make_unique<Face>(make_unique<DummyLinkService>(),
65 make_unique<UnicastUdpTransport>(std::move(sock), persistency, 3_s));
Eric Newberry65caf202015-12-17 15:08:16 -070066 transport = static_cast<UnicastUdpTransport*>(face->getTransport());
Davide Pesaventoa8098582019-03-31 15:48:02 -040067 receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
Eric Newberry65caf202015-12-17 15:08:16 -070068
69 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
70 }
71
72 void
73 remoteConnect(ip::address address = ip::address_v4::loopback())
74 {
75 udp::endpoint remoteEp(address, 7070);
76 remoteSocket.open(remoteEp.protocol());
77 remoteSocket.set_option(udp::socket::reuse_address(true));
78 remoteSocket.bind(remoteEp);
79 remoteSocket.connect(localEp);
80 }
81
82 void
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070083 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
84 {
85 remoteSocket.async_receive(boost::asio::buffer(buf),
86 [this, needToCheck] (const boost::system::error_code& error, size_t) {
87 if (needToCheck) {
88 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
89 }
90 limitedIo.afterOp();
91 });
Davide Pesavento14e71f02019-03-28 17:35:25 -040092 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070093 }
94
95 void
Eric Newberry65caf202015-12-17 15:08:16 -070096 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
97 {
98 remoteSocket.async_send(boost::asio::buffer(buf),
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040099 [needToCheck] (const auto& error, size_t) {
Eric Newberry65caf202015-12-17 15:08:16 -0700100 if (needToCheck) {
101 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
102 }
103 });
Davide Pesavento14e71f02019-03-28 17:35:25 -0400104 limitedIo.defer(1_s);
Eric Newberry65caf202015-12-17 15:08:16 -0700105 }
106
107protected:
108 LimitedIo limitedIo;
109 UnicastUdpTransport* transport;
110 udp::endpoint localEp;
111 udp::socket remoteSocket;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400112 std::vector<RxPacket>* receivedPackets;
Eric Newberry65caf202015-12-17 15:08:16 -0700113
114private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700115 unique_ptr<Face> face;
Eric Newberry65caf202015-12-17 15:08:16 -0700116};
117
118} // namespace tests
119} // namespace face
120} // namespace nfd
121
122#endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP