blob: dcd99ef1793555c8b65a4e15dd07f14779d9d144 [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 Pesaventocf7db2f2019-03-24 23:17:28 -040034#include "dummy-receive-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
Junxiao Shicde37ad2015-12-24 01:02:05 -070064 face = make_unique<Face>(
65 make_unique<DummyReceiveLinkService>(),
Davide Pesavento14e71f02019-03-28 17:35:25 -040066 make_unique<UnicastUdpTransport>(std::move(sock), persistency, 3_s));
Eric Newberry65caf202015-12-17 15:08:16 -070067 transport = static_cast<UnicastUdpTransport*>(face->getTransport());
68 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
69
70 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
71 }
72
73 void
74 remoteConnect(ip::address address = ip::address_v4::loopback())
75 {
76 udp::endpoint remoteEp(address, 7070);
77 remoteSocket.open(remoteEp.protocol());
78 remoteSocket.set_option(udp::socket::reuse_address(true));
79 remoteSocket.bind(remoteEp);
80 remoteSocket.connect(localEp);
81 }
82
83 void
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070084 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
85 {
86 remoteSocket.async_receive(boost::asio::buffer(buf),
87 [this, needToCheck] (const boost::system::error_code& error, size_t) {
88 if (needToCheck) {
89 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
90 }
91 limitedIo.afterOp();
92 });
Davide Pesavento14e71f02019-03-28 17:35:25 -040093 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070094 }
95
96 void
Eric Newberry65caf202015-12-17 15:08:16 -070097 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
98 {
99 remoteSocket.async_send(boost::asio::buffer(buf),
100 [needToCheck] (const boost::system::error_code& error, size_t) {
101 if (needToCheck) {
102 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
103 }
104 });
Davide Pesavento14e71f02019-03-28 17:35:25 -0400105 limitedIo.defer(1_s);
Eric Newberry65caf202015-12-17 15:08:16 -0700106 }
107
108protected:
109 LimitedIo limitedIo;
110 UnicastUdpTransport* transport;
111 udp::endpoint localEp;
112 udp::socket remoteSocket;
113 std::vector<Transport::Packet>* receivedPackets;
114
115private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700116 unique_ptr<Face> face;
Eric Newberry65caf202015-12-17 15:08:16 -0700117};
118
119} // namespace tests
120} // namespace face
121} // namespace nfd
122
123#endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP