blob: 1ec37b0d6429f964a80d68564d39f7f6bc35a7c0 [file] [log] [blame]
Eric Newberry65caf202015-12-17 15:08:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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"
33#include "tests/limited-io.hpp"
34
35namespace nfd {
36namespace face {
37namespace tests {
38
39using namespace nfd::tests;
40namespace ip = boost::asio::ip;
41using ip::udp;
42
43class UnicastUdpTransportFixture : public BaseFixture
44{
45protected:
46 UnicastUdpTransportFixture()
47 : transport(nullptr)
48 , remoteSocket(g_io)
49 , receivedPackets(nullptr)
50 {
51 }
52
53 void
54 initialize(ip::address address = ip::address_v4::loopback(),
55 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
56 {
57 udp::socket sock(g_io);
58 sock.connect(udp::endpoint(address, 7070));
59 localEp = sock.local_endpoint();
60
61 remoteConnect(address);
62
Junxiao Shicde37ad2015-12-24 01:02:05 -070063 face = make_unique<Face>(
64 make_unique<DummyReceiveLinkService>(),
65 make_unique<UnicastUdpTransport>(std::move(sock), persistency, time::seconds(3)));
Eric Newberry65caf202015-12-17 15:08:16 -070066 transport = static_cast<UnicastUdpTransport*>(face->getTransport());
67 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
68
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
83 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
84 {
85 remoteSocket.async_send(boost::asio::buffer(buf),
86 [needToCheck] (const boost::system::error_code& error, size_t) {
87 if (needToCheck) {
88 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
89 }
90 });
91 limitedIo.defer(time::seconds(1));
92 }
93
94protected:
95 LimitedIo limitedIo;
96 UnicastUdpTransport* transport;
97 udp::endpoint localEp;
98 udp::socket remoteSocket;
99 std::vector<Transport::Packet>* receivedPackets;
100
101private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700102 unique_ptr<Face> face;
Eric Newberry65caf202015-12-17 15:08:16 -0700103};
104
105} // namespace tests
106} // namespace face
107} // namespace nfd
108
109#endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP