Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -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 | /* |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -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 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 26 | #include "unicast-udp-transport-fixture.hpp" |
| 27 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 28 | #include <boost/mpl/vector.hpp> |
| 29 | #include <boost/mpl/vector_c.hpp> |
| 30 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::tests { |
| 32 | |
| 33 | using namespace nfd::face; |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 34 | |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(Face) |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TestUnicastUdpTransport, IpTransportFixture<UnicastUdpTransportFixture>) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 37 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 38 | using UnicastUdpTransportFixtures = boost::mpl::vector< |
| 39 | GENERATE_IP_TRANSPORT_FIXTURE_INSTANTIATIONS(UnicastUdpTransportFixture) |
| 40 | >; |
| 41 | |
| 42 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(StaticProperties, T, UnicastUdpTransportFixtures, T) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 43 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 44 | TRANSPORT_TEST_INIT(); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 45 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 46 | checkStaticPropertiesInitialized(*this->transport); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 47 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(this->transport->getLocalUri(), FaceUri(udp::endpoint(this->address, this->localEp.port()))); |
| 49 | BOOST_CHECK_EQUAL(this->transport->getRemoteUri(), FaceUri(udp::endpoint(this->address, 7070))); |
| 50 | BOOST_CHECK_EQUAL(this->transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local |
| 51 | BOOST_CHECK_EQUAL(this->transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 52 | BOOST_CHECK_EQUAL(this->transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 53 | BOOST_CHECK_EQUAL(this->transport->getMtu(), |
| 54 | this->addressFamily == AddressFamily::V4 ? (65535 - 60 - 8) : (65535 - 8)); |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 55 | BOOST_CHECK_GT(this->transport->getSendQueueCapacity(), 0); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 58 | BOOST_AUTO_TEST_CASE(PersistencyChange) |
| 59 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 60 | TRANSPORT_TEST_INIT(); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 61 | |
| 62 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND), true); |
| 63 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERSISTENT), true); |
| 64 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true); |
| 65 | } |
| 66 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 67 | BOOST_AUTO_TEST_CASE(ExpirationTime) |
| 68 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 69 | TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 70 | BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::time_point::max()); |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 71 | |
| 72 | transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::time_point::max()); |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 74 | |
| 75 | transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 76 | BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::time_point::max()); |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 79 | BOOST_AUTO_TEST_CASE(IdleClose) |
| 80 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 81 | TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 82 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 83 | int nStateChanges = 0; |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 84 | transport->afterStateChange.connect( |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 85 | [this, &nStateChanges] (auto oldState, auto newState) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 86 | switch (nStateChanges) { |
| 87 | case 0: |
| 88 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 89 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSING); |
| 90 | break; |
| 91 | case 1: |
| 92 | BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING); |
| 93 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSED); |
| 94 | break; |
| 95 | default: |
| 96 | BOOST_CHECK(false); |
| 97 | } |
| 98 | nStateChanges++; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 99 | this->limitedIo.afterOp(); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 100 | }); |
| 101 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 102 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, 8_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(nStateChanges, 2); |
| 104 | } |
| 105 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 106 | using RemoteCloseFixture = IpTransportFixture<UnicastUdpTransportFixture, |
| 107 | AddressFamily::Any, AddressScope::Loopback>; |
| 108 | using RemoteClosePersistencies = boost::mpl::vector_c<ndn::nfd::FacePersistency, |
| 109 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
| 110 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT>; |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 111 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 112 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, Persistency, RemoteClosePersistencies, RemoteCloseFixture) |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 113 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 114 | TRANSPORT_TEST_INIT(Persistency::value); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 115 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 116 | transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 118 | BOOST_CHECK_EQUAL(newState, TransportState::FAILED); |
| 119 | this->limitedIo.afterOp(); |
| 120 | }); |
| 121 | |
| 122 | remoteSocket.close(); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 123 | transport->send(ndn::encoding::makeStringBlock(300, "hello")); // trigger ICMP error |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 124 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 125 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 126 | transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) { |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL(oldState, TransportState::FAILED); |
| 128 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSED); |
| 129 | this->limitedIo.afterOp(); |
| 130 | }); |
| 131 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 132 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 135 | BOOST_FIXTURE_TEST_CASE(RemoteClosePermanent, RemoteCloseFixture) |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 136 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 137 | TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 138 | |
| 139 | remoteSocket.close(); |
| 140 | |
| 141 | Block block1 = ndn::encoding::makeStringBlock(300, "hello"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 142 | transport->send(block1); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 1); |
| 144 | BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, block1.size()); |
| 145 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 146 | limitedIo.defer(1_s); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 147 | BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP); |
| 148 | |
| 149 | remoteConnect(); |
| 150 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 151 | transport->send(block1); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 152 | BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2); |
| 153 | BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, 2 * block1.size()); |
| 154 | |
| 155 | std::vector<uint8_t> readBuf(block1.size()); |
| 156 | remoteSocket.async_receive(boost::asio::buffer(readBuf), |
| 157 | [this] (const boost::system::error_code& error, size_t) { |
| 158 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 159 | limitedIo.afterOp(); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 160 | }); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 161 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 162 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 163 | BOOST_TEST(readBuf == block1, boost::test_tools::per_element()); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 164 | |
| 165 | Block block2 = ndn::encoding::makeStringBlock(301, "world"); |
| 166 | ndn::Buffer buf(block2.begin(), block2.end()); |
| 167 | remoteWrite(buf); |
| 168 | |
| 169 | BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 1); |
| 170 | BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, block2.size()); |
| 171 | BOOST_CHECK_EQUAL(receivedPackets->size(), 1); |
| 172 | BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 175 | BOOST_AUTO_TEST_SUITE_END() // TestUnicastUdpTransport |
| 176 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 177 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 178 | } // namespace nfd::tests |