Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [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 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, 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 | #include "tcp-channel-fixture.hpp" |
| 27 | #include "udp-channel-fixture.hpp" |
| 28 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 29 | #include "test-ip.hpp" |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 30 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 31 | #include <boost/mpl/vector.hpp> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 33 | namespace nfd::tests { |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(Face) |
| 36 | BOOST_AUTO_TEST_SUITE(TestTcpUdpChannel) |
| 37 | |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 38 | template<typename F, AddressFamily AF> |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 39 | struct FixtureAndAddress |
| 40 | { |
| 41 | using Fixture = F; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 42 | static constexpr AddressFamily ADDRESS_FAMILY = AF; |
| 43 | using Address = typename IpAddressFromFamily<AF>::type; |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | using FixtureAndAddressList = boost::mpl::vector< |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 47 | FixtureAndAddress<TcpChannelFixture, AddressFamily::V4>, |
| 48 | FixtureAndAddress<TcpChannelFixture, AddressFamily::V6>, |
| 49 | FixtureAndAddress<UdpChannelFixture, AddressFamily::V4>, |
| 50 | FixtureAndAddress<UdpChannelFixture, AddressFamily::V6> |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 51 | >; |
| 52 | |
| 53 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Uri, T, FixtureAndAddressList, T::Fixture) |
| 54 | { |
| 55 | decltype(this->listenerEp) ep(T::Address::loopback(), 7050); |
| 56 | auto channel = this->makeChannel(ep.address(), ep.port()); |
| 57 | BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep)); |
| 58 | } |
| 59 | |
| 60 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Listen, T, FixtureAndAddressList, T::Fixture) |
| 61 | { |
| 62 | auto channel = this->makeChannel(typename T::Address()); |
| 63 | BOOST_CHECK_EQUAL(channel->isListening(), false); |
| 64 | |
| 65 | channel->listen(nullptr, nullptr); |
| 66 | BOOST_CHECK_EQUAL(channel->isListening(), true); |
| 67 | |
| 68 | // listen() is idempotent |
| 69 | BOOST_CHECK_NO_THROW(channel->listen(nullptr, nullptr)); |
| 70 | BOOST_CHECK_EQUAL(channel->isListening(), true); |
| 71 | } |
| 72 | |
| 73 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(MultipleAccepts, T, FixtureAndAddressList, T::Fixture) |
| 74 | { |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 75 | auto address = getTestIp(T::ADDRESS_FAMILY, AddressScope::Loopback); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 76 | SKIP_IF_IP_UNAVAILABLE(address); |
| 77 | this->listen(address); |
| 78 | |
| 79 | BOOST_CHECK_EQUAL(this->listenerChannel->isListening(), true); |
| 80 | BOOST_CHECK_EQUAL(this->listenerChannel->size(), 0); |
| 81 | |
| 82 | auto ch1 = this->makeChannel(typename T::Address()); |
| 83 | this->connect(*ch1); |
| 84 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(this->limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 86 | |
| 87 | BOOST_CHECK_EQUAL(this->listenerChannel->size(), 1); |
| 88 | BOOST_CHECK_EQUAL(ch1->size(), 1); |
| 89 | BOOST_CHECK_EQUAL(ch1->isListening(), false); |
| 90 | |
| 91 | auto ch2 = this->makeChannel(typename T::Address()); |
| 92 | auto ch3 = this->makeChannel(typename T::Address()); |
| 93 | this->connect(*ch2); |
| 94 | this->connect(*ch3); |
| 95 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(this->limitedIo.run(4, 2_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 97 | |
| 98 | BOOST_CHECK_EQUAL(this->listenerChannel->size(), 3); |
| 99 | BOOST_CHECK_EQUAL(ch1->size(), 1); |
| 100 | BOOST_CHECK_EQUAL(ch2->size(), 1); |
| 101 | BOOST_CHECK_EQUAL(ch3->size(), 1); |
| 102 | BOOST_CHECK_EQUAL(this->clientFaces.size(), 3); |
| 103 | |
| 104 | // check face persistency |
| 105 | for (const auto& face : this->listenerFaces) { |
| 106 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 107 | } |
| 108 | for (const auto& face : this->clientFaces) { |
| 109 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 110 | } |
| 111 | |
| 112 | // connect twice to the same endpoint |
| 113 | this->connect(*ch3); |
| 114 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 116 | |
| 117 | BOOST_CHECK_EQUAL(this->listenerChannel->size(), 3); |
| 118 | BOOST_CHECK_EQUAL(ch1->size(), 1); |
| 119 | BOOST_CHECK_EQUAL(ch2->size(), 1); |
| 120 | BOOST_CHECK_EQUAL(ch3->size(), 1); |
| 121 | BOOST_CHECK_EQUAL(this->clientFaces.size(), 4); |
| 122 | BOOST_CHECK_EQUAL(this->clientFaces.at(2), this->clientFaces.at(3)); |
| 123 | } |
| 124 | |
| 125 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(FaceClosure, T, FixtureAndAddressList, T::Fixture) |
| 126 | { |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 127 | auto address = getTestIp(T::ADDRESS_FAMILY, AddressScope::Loopback); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 128 | SKIP_IF_IP_UNAVAILABLE(address); |
| 129 | this->listen(address); |
| 130 | |
| 131 | auto clientChannel = this->makeChannel(typename T::Address()); |
| 132 | this->connect(*clientChannel); |
| 133 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(this->limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 135 | |
| 136 | BOOST_CHECK_EQUAL(this->listenerChannel->size(), 1); |
| 137 | BOOST_CHECK_EQUAL(clientChannel->size(), 1); |
| 138 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(this->clientFaces.at(0)->getChannel().lock(), clientChannel); |
| 140 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 141 | this->clientFaces.at(0)->close(); |
| 142 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(this->limitedIo.run(2, 5_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 144 | |
| 145 | BOOST_CHECK_EQUAL(this->listenerChannel->size(), 0); |
| 146 | BOOST_CHECK_EQUAL(clientChannel->size(), 0); |
| 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_SUITE_END() // TestTcpUdpChannel |
| 150 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 151 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 152 | } // namespace nfd::tests |