blob: 01698cb7aca57e8dcaafec5c2da8b05d22224da8 [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)
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070049 , defaultAddr(ip::address_v4::loopback())
Eric Newberry65caf202015-12-17 15:08:16 -070050 , receivedPackets(nullptr)
51 {
52 }
53
54 void
55 initialize(ip::address address = ip::address_v4::loopback(),
56 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>(),
66 make_unique<UnicastUdpTransport>(std::move(sock), persistency, time::seconds(3)));
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 });
93 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
94 }
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 });
105 limitedIo.defer(time::seconds(1));
106 }
107
108protected:
109 LimitedIo limitedIo;
110 UnicastUdpTransport* transport;
111 udp::endpoint localEp;
112 udp::socket remoteSocket;
Eric Newberry6d8ee7a2015-12-21 16:37:52 -0700113 const ip::address defaultAddr;
Eric Newberry65caf202015-12-17 15:08:16 -0700114 std::vector<Transport::Packet>* receivedPackets;
115
116private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700117 unique_ptr<Face> face;
Eric Newberry65caf202015-12-17 15:08:16 -0700118};
119
120} // namespace tests
121} // namespace face
122} // namespace nfd
123
124#endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP