Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [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_CHANNEL_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_TCP_CHANNEL_FIXTURE_HPP |
| 28 | |
| 29 | #include "face/tcp-channel.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 30 | #include "face/network-predicate.hpp" |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 31 | |
| 32 | #include "channel-fixture.hpp" |
| 33 | |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 34 | #include <boost/asio/defer.hpp> |
| 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tests { |
| 37 | |
| 38 | using face::TcpChannel; |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 39 | |
| 40 | class TcpChannelFixture : public ChannelFixture<TcpChannel, tcp::Endpoint> |
| 41 | { |
| 42 | protected: |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 43 | TcpChannelFixture() |
| 44 | { |
| 45 | local.assign({{"subnet", "127.0.0.0/8"}, {"subnet", "::1/128"}}, {}); |
| 46 | } |
| 47 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 48 | shared_ptr<TcpChannel> |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 49 | makeChannel(const boost::asio::ip::address& addr, uint16_t port = 0, |
| 50 | std::optional<size_t> mtu = std::nullopt) final |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 51 | { |
| 52 | if (port == 0) |
| 53 | port = getNextPort(); |
| 54 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 55 | return std::make_shared<TcpChannel>(tcp::Endpoint(addr, port), false, |
| 56 | std::bind(&TcpChannelFixture::determineFaceScope, this, _1, _2)); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 59 | void |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 60 | connect(TcpChannel& channel) final |
| 61 | { |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 62 | boost::asio::defer(g_io, [&] { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 63 | channel.connect(listenerEp, {}, |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 64 | [this] (const shared_ptr<Face>& newFace) { |
| 65 | BOOST_REQUIRE(newFace != nullptr); |
| 66 | connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); }); |
| 67 | clientFaces.push_back(newFace); |
| 68 | limitedIo.afterOp(); |
| 69 | }, |
| 70 | ChannelFixture::unexpectedFailure); |
| 71 | }); |
| 72 | } |
| 73 | |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 74 | ndn::nfd::FaceScope |
| 75 | determineFaceScope(const boost::asio::ip::address& localAddress, |
| 76 | const boost::asio::ip::address& remoteAddress) |
| 77 | { |
| 78 | if (local(localAddress) && local(remoteAddress)) { |
| 79 | return ndn::nfd::FACE_SCOPE_LOCAL; |
| 80 | } |
| 81 | else { |
| 82 | return ndn::nfd::FACE_SCOPE_NON_LOCAL; |
| 83 | } |
| 84 | } |
| 85 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 86 | protected: |
| 87 | std::vector<shared_ptr<Face>> clientFaces; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 88 | face::IpAddressPredicate local; |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 91 | } // namespace nfd::tests |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 92 | |
| 93 | #endif // NFD_TESTS_DAEMON_FACE_TCP_CHANNEL_FIXTURE_HPP |