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" |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 33 | #include "test-netif-ip.hpp" |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 34 | #include "tests/limited-io.hpp" |
| 35 | |
| 36 | namespace nfd { |
| 37 | namespace face { |
| 38 | namespace tests { |
| 39 | |
| 40 | using namespace nfd::tests; |
| 41 | namespace ip = boost::asio::ip; |
| 42 | using ip::udp; |
| 43 | |
| 44 | class UnicastUdpTransportFixture : public BaseFixture |
| 45 | { |
| 46 | protected: |
| 47 | UnicastUdpTransportFixture() |
| 48 | : transport(nullptr) |
| 49 | , remoteSocket(g_io) |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 50 | , defaultAddr(getTestIp<AddressFamily::V4>()) |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 51 | , 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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 65 | face = make_unique<Face>( |
| 66 | make_unique<DummyReceiveLinkService>(), |
| 67 | make_unique<UnicastUdpTransport>(std::move(sock), persistency, time::seconds(3))); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 68 | 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 Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 85 | 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 Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 98 | 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 | |
| 109 | protected: |
| 110 | LimitedIo limitedIo; |
| 111 | UnicastUdpTransport* transport; |
| 112 | udp::endpoint localEp; |
| 113 | udp::socket remoteSocket; |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame] | 114 | const ip::address defaultAddr; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 115 | std::vector<Transport::Packet>* receivedPackets; |
| 116 | |
| 117 | private: |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 118 | unique_ptr<Face> face; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // namespace tests |
| 122 | } // namespace face |
| 123 | } // namespace nfd |
| 124 | |
| 125 | #endif // NFD_TESTS_DAEMON_FACE_UNICAST_UDP_TRANSPORT_FIXTURE_HPP |