Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -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_TCP_TRANSPORT_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP |
| 28 | |
| 29 | #include "face/tcp-transport.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | #include "face/face.hpp" |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 31 | |
| 32 | #include "dummy-receive-link-service.hpp" |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 33 | #include "test-ip.hpp" |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -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::tcp; |
| 43 | |
| 44 | class TcpTransportFixture : public BaseFixture |
| 45 | { |
| 46 | protected: |
| 47 | TcpTransportFixture() |
| 48 | : transport(nullptr) |
| 49 | , remoteSocket(g_io) |
| 50 | , receivedPackets(nullptr) |
| 51 | , acceptor(g_io) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame^] | 56 | startAccept(const tcp::endpoint& remoteEp) |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 57 | { |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame^] | 58 | BOOST_REQUIRE(!acceptor.is_open()); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 59 | acceptor.open(remoteEp.protocol()); |
| 60 | acceptor.set_option(tcp::acceptor::reuse_address(true)); |
| 61 | acceptor.bind(remoteEp); |
| 62 | acceptor.listen(1); |
| 63 | acceptor.async_accept(remoteSocket, [this] (const boost::system::error_code& error) { |
| 64 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 65 | limitedIo.afterOp(); |
| 66 | }); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame^] | 67 | } |
| 68 | |
| 69 | void |
| 70 | stopAccept() |
| 71 | { |
| 72 | BOOST_REQUIRE(acceptor.is_open()); |
| 73 | acceptor.close(); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | initialize(ip::address address = ip::address_v4::loopback(), |
| 78 | ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT) |
| 79 | { |
| 80 | tcp::endpoint remoteEp(address, 7070); |
| 81 | startAccept(remoteEp); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 82 | |
| 83 | tcp::socket sock(g_io); |
| 84 | sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) { |
| 85 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 86 | limitedIo.afterOp(); |
| 87 | }); |
| 88 | |
| 89 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 90 | |
| 91 | localEp = sock.local_endpoint(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 92 | face = make_unique<Face>(make_unique<DummyReceiveLinkService>(), |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame^] | 93 | make_unique<TcpTransport>(std::move(sock), persistency)); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 94 | transport = static_cast<TcpTransport*>(face->getTransport()); |
| 95 | receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets; |
| 96 | |
| 97 | BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP); |
| 98 | } |
| 99 | |
| 100 | void |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 101 | remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true) |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 102 | { |
| 103 | boost::asio::async_write(remoteSocket, boost::asio::buffer(buf), |
| 104 | [needToCheck] (const boost::system::error_code& error, size_t) { |
| 105 | if (needToCheck) { |
| 106 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 107 | } |
| 108 | }); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 109 | limitedIo.defer(time::seconds(1)); |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | protected: |
| 113 | LimitedIo limitedIo; |
| 114 | TcpTransport* transport; |
| 115 | tcp::endpoint localEp; |
| 116 | tcp::socket remoteSocket; |
| 117 | std::vector<Transport::Packet>* receivedPackets; |
| 118 | |
| 119 | private: |
| 120 | tcp::acceptor acceptor; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 121 | unique_ptr<Face> face; |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace tests |
| 125 | } // namespace face |
| 126 | } // namespace nfd |
| 127 | |
| 128 | #endif // NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP |