Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | #include "face/face.hpp" |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 31 | |
| 32 | #include "dummy-receive-link-service.hpp" |
| 33 | #include "tests/limited-io.hpp" |
| 34 | |
| 35 | namespace nfd { |
| 36 | namespace face { |
| 37 | namespace tests { |
| 38 | |
| 39 | using namespace nfd::tests; |
| 40 | namespace ip = boost::asio::ip; |
| 41 | using ip::udp; |
| 42 | |
| 43 | class UnicastUdpTransportFixture : public BaseFixture |
| 44 | { |
| 45 | protected: |
| 46 | UnicastUdpTransportFixture() |
| 47 | : transport(nullptr) |
| 48 | , remoteSocket(g_io) |
| 49 | , receivedPackets(nullptr) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | void |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 54 | initialize(ip::address address, |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 63 | face = make_unique<Face>( |
| 64 | make_unique<DummyReceiveLinkService>(), |
| 65 | make_unique<UnicastUdpTransport>(std::move(sock), persistency, time::seconds(3))); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 66 | 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 |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 83 | 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 | }); |
| 92 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 93 | } |
| 94 | |
| 95 | void |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 96 | remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true) |
| 97 | { |
| 98 | remoteSocket.async_send(boost::asio::buffer(buf), |
| 99 | [needToCheck] (const boost::system::error_code& error, size_t) { |
| 100 | if (needToCheck) { |
| 101 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 102 | } |
| 103 | }); |
| 104 | limitedIo.defer(time::seconds(1)); |
| 105 | } |
| 106 | |
| 107 | protected: |
| 108 | LimitedIo limitedIo; |
| 109 | UnicastUdpTransport* transport; |
| 110 | udp::endpoint localEp; |
| 111 | udp::socket remoteSocket; |
| 112 | std::vector<Transport::Packet>* receivedPackets; |
| 113 | |
| 114 | private: |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 115 | unique_ptr<Face> face; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // namespace tests |
| 119 | } // namespace face |
| 120 | } // namespace nfd |
| 121 | |
| 122 | #endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP |