blob: 0cfae444c0cf6065455477873b3888753a454e23 [file] [log] [blame]
Eric Newberryf68f3782015-12-11 00:31:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
Alexander Afanasyevded17422018-04-03 19:00:23 -04003 * Copyright (c) 2014-2018, Regents of the University of California,
Eric Newberryf68f3782015-12-11 00:31:32 -07004 * 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 Shicde37ad2015-12-24 01:02:05 -070030#include "face/face.hpp"
Eric Newberryf68f3782015-12-11 00:31:32 -070031
32#include "dummy-receive-link-service.hpp"
Eric Newberryf68f3782015-12-11 00:31:32 -070033#include "tests/limited-io.hpp"
34
35namespace nfd {
36namespace face {
37namespace tests {
38
39using namespace nfd::tests;
40namespace ip = boost::asio::ip;
41using ip::tcp;
42
43class TcpTransportFixture : public BaseFixture
44{
45protected:
46 TcpTransportFixture()
47 : transport(nullptr)
48 , remoteSocket(g_io)
49 , receivedPackets(nullptr)
50 , acceptor(g_io)
51 {
52 }
53
54 void
Weiwei Liudcdf6212016-08-31 14:34:22 -070055 startAccept(const tcp::endpoint& remoteEp)
Eric Newberryf68f3782015-12-11 00:31:32 -070056 {
Weiwei Liudcdf6212016-08-31 14:34:22 -070057 BOOST_REQUIRE(!acceptor.is_open());
Eric Newberryf68f3782015-12-11 00:31:32 -070058 acceptor.open(remoteEp.protocol());
59 acceptor.set_option(tcp::acceptor::reuse_address(true));
60 acceptor.bind(remoteEp);
61 acceptor.listen(1);
62 acceptor.async_accept(remoteSocket, [this] (const boost::system::error_code& error) {
63 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
64 limitedIo.afterOp();
65 });
Weiwei Liudcdf6212016-08-31 14:34:22 -070066 }
67
68 void
69 stopAccept()
70 {
71 BOOST_REQUIRE(acceptor.is_open());
72 acceptor.close();
73 }
74
75 void
Davide Pesavento22fba352017-10-17 15:53:51 -040076 initialize(ip::address address,
Weiwei Liudcdf6212016-08-31 14:34:22 -070077 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
78 {
79 tcp::endpoint remoteEp(address, 7070);
80 startAccept(remoteEp);
Eric Newberryf68f3782015-12-11 00:31:32 -070081
82 tcp::socket sock(g_io);
83 sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) {
84 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
85 limitedIo.afterOp();
86 });
87
88 BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS);
89
90 localEp = sock.local_endpoint();
Alexander Afanasyevded17422018-04-03 19:00:23 -040091
92 ndn::nfd::FaceScope scope;
93 if (sock.local_endpoint().address().is_loopback() && sock.remote_endpoint().address().is_loopback()) {
94 scope = ndn::nfd::FACE_SCOPE_LOCAL;
95 }
96 else {
97 scope = ndn::nfd::FACE_SCOPE_NON_LOCAL;
98 }
99
Junxiao Shicde37ad2015-12-24 01:02:05 -0700100 face = make_unique<Face>(make_unique<DummyReceiveLinkService>(),
Alexander Afanasyevded17422018-04-03 19:00:23 -0400101 make_unique<TcpTransport>(std::move(sock), persistency, scope));
Eric Newberryf68f3782015-12-11 00:31:32 -0700102 transport = static_cast<TcpTransport*>(face->getTransport());
103 receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
104
105 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
106 }
107
108 void
Eric Newberry65caf202015-12-17 15:08:16 -0700109 remoteWrite(const std::vector<uint8_t>& buf, bool needToCheck = true)
Eric Newberryf68f3782015-12-11 00:31:32 -0700110 {
111 boost::asio::async_write(remoteSocket, boost::asio::buffer(buf),
112 [needToCheck] (const boost::system::error_code& error, size_t) {
113 if (needToCheck) {
114 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
115 }
116 });
Eric Newberry65caf202015-12-17 15:08:16 -0700117 limitedIo.defer(time::seconds(1));
Eric Newberryf68f3782015-12-11 00:31:32 -0700118 }
119
120protected:
121 LimitedIo limitedIo;
122 TcpTransport* transport;
123 tcp::endpoint localEp;
124 tcp::socket remoteSocket;
125 std::vector<Transport::Packet>* receivedPackets;
126
127private:
128 tcp::acceptor acceptor;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700129 unique_ptr<Face> face;
Eric Newberryf68f3782015-12-11 00:31:32 -0700130};
131
132} // namespace tests
133} // namespace face
134} // namespace nfd
135
136#endif // NFD_TESTS_DAEMON_FACE_TCP_TRANSPORT_FIXTURE_HPP