blob: ef8f5885c1ecaad2d75714065489c25ffb3e5d4a [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/*
3 * Copyright (c) 2014-2017, 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
32#include "dummy-receive-link-service.hpp"
Junxiao Shi84d62cb2017-07-12 16:15:18 +000033#include "test-netif-ip.hpp"
Eric Newberry65caf202015-12-17 15:08:16 -070034#include "tests/limited-io.hpp"
35
36namespace nfd {
37namespace face {
38namespace tests {
39
40using namespace nfd::tests;
41namespace ip = boost::asio::ip;
42using ip::udp;
43
44class UnicastUdpTransportFixture : public BaseFixture
45{
46protected:
47 UnicastUdpTransportFixture()
48 : transport(nullptr)
49 , remoteSocket(g_io)
Junxiao Shi84d62cb2017-07-12 16:15:18 +000050 , defaultAddr(getTestIp<AddressFamily::V4>())
Eric Newberry65caf202015-12-17 15:08:16 -070051 , receivedPackets(nullptr)
52 {
53 }
54
55 void
56 initialize(ip::address address = ip::address_v4::loopback(),
57 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
58 {
59 udp::socket sock(g_io);
60 sock.connect(udp::endpoint(address, 7070));
61 localEp = sock.local_endpoint();
62
63 remoteConnect(address);
64
Junxiao Shicde37ad2015-12-24 01:02:05 -070065 face = make_unique<Face>(
66 make_unique<DummyReceiveLinkService>(),
67 make_unique<UnicastUdpTransport>(std::move(sock), persistency, time::seconds(3)));
Eric Newberry65caf202015-12-17 15:08:16 -070068 transport = static_cast<UnicastUdpTransport*>(face->getTransport());
69 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
70
71 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
72 }
73
74 void
75 remoteConnect(ip::address address = ip::address_v4::loopback())
76 {
77 udp::endpoint remoteEp(address, 7070);
78 remoteSocket.open(remoteEp.protocol());
79 remoteSocket.set_option(udp::socket::reuse_address(true));
80 remoteSocket.bind(remoteEp);
81 remoteSocket.connect(localEp);
82 }
83
84 void
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070085 remoteRead(std::vector<uint8_t>& buf, bool needToCheck = true)
86 {
87 remoteSocket.async_receive(boost::asio::buffer(buf),
88 [this, needToCheck] (const boost::system::error_code& error, size_t) {
89 if (needToCheck) {
90 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
91 }
92 limitedIo.afterOp();
93 });
94 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
95 }
96
97 void
Eric Newberry65caf202015-12-17 15:08:16 -070098 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
99 {
100 remoteSocket.async_send(boost::asio::buffer(buf),
101 [needToCheck] (const boost::system::error_code& error, size_t) {
102 if (needToCheck) {
103 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
104 }
105 });
106 limitedIo.defer(time::seconds(1));
107 }
108
109protected:
110 LimitedIo limitedIo;
111 UnicastUdpTransport* transport;
112 udp::endpoint localEp;
113 udp::socket remoteSocket;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700114 const ip::address defaultAddr;
Eric Newberry65caf202015-12-17 15:08:16 -0700115 std::vector<Transport::Packet>* receivedPackets;
116
117private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700118 unique_ptr<Face> face;
Eric Newberry65caf202015-12-17 15:08:16 -0700119};
120
121} // namespace tests
122} // namespace face
123} // namespace nfd
124
125#endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP