blob: 144fed59c599e0bf02b5cbf6740761e94ae178d2 [file] [log] [blame]
Junxiao Shi13546112015-10-14 19:33:07 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
Davide Pesaventof56cf632024-01-27 22:22:06 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shi13546112015-10-14 19:33:07 -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
Eric Newberry65caf202015-12-17 15:08:16 -070026#include "unicast-udp-transport-fixture.hpp"
27
Davide Pesaventof56cf632024-01-27 22:22:06 -050028#include <boost/mp11/list.hpp>
Davide Pesavento22fba352017-10-17 15:53:51 -040029
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::tests {
31
32using namespace nfd::face;
Junxiao Shi13546112015-10-14 19:33:07 -070033
Junxiao Shi13546112015-10-14 19:33:07 -070034BOOST_AUTO_TEST_SUITE(Face)
Davide Pesavento22fba352017-10-17 15:53:51 -040035BOOST_FIXTURE_TEST_SUITE(TestUnicastUdpTransport, IpTransportFixture<UnicastUdpTransportFixture>)
Junxiao Shi13546112015-10-14 19:33:07 -070036
Davide Pesaventof56cf632024-01-27 22:22:06 -050037using UnicastUdpTransportFixtures = boost::mp11::mp_list<
Davide Pesavento22fba352017-10-17 15:53:51 -040038 GENERATE_IP_TRANSPORT_FIXTURE_INSTANTIATIONS(UnicastUdpTransportFixture)
39>;
40
41BOOST_FIXTURE_TEST_CASE_TEMPLATE(StaticProperties, T, UnicastUdpTransportFixtures, T)
Junxiao Shi13546112015-10-14 19:33:07 -070042{
Davide Pesavento22fba352017-10-17 15:53:51 -040043 TRANSPORT_TEST_INIT();
Junxiao Shi13546112015-10-14 19:33:07 -070044
Davide Pesavento22fba352017-10-17 15:53:51 -040045 checkStaticPropertiesInitialized(*this->transport);
Eric Newberry65caf202015-12-17 15:08:16 -070046
Davide Pesavento22fba352017-10-17 15:53:51 -040047 BOOST_CHECK_EQUAL(this->transport->getLocalUri(), FaceUri(udp::endpoint(this->address, this->localEp.port())));
48 BOOST_CHECK_EQUAL(this->transport->getRemoteUri(), FaceUri(udp::endpoint(this->address, 7070)));
49 BOOST_CHECK_EQUAL(this->transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local
50 BOOST_CHECK_EQUAL(this->transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
51 BOOST_CHECK_EQUAL(this->transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
52 BOOST_CHECK_EQUAL(this->transport->getMtu(),
53 this->addressFamily == AddressFamily::V4 ? (65535 - 60 - 8) : (65535 - 8));
Eric Newberryb49313d2017-12-24 20:22:27 -070054 BOOST_CHECK_GT(this->transport->getSendQueueCapacity(), 0);
Eric Newberry65caf202015-12-17 15:08:16 -070055}
56
Davide Pesavento32065652017-01-15 01:52:21 -050057BOOST_AUTO_TEST_CASE(PersistencyChange)
58{
Davide Pesavento22fba352017-10-17 15:53:51 -040059 TRANSPORT_TEST_INIT();
Davide Pesavento32065652017-01-15 01:52:21 -050060
61 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND), true);
62 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERSISTENT), true);
63 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true);
64}
65
Davide Pesavento1816d4b2017-07-02 12:20:48 -040066BOOST_AUTO_TEST_CASE(ExpirationTime)
67{
Davide Pesavento22fba352017-10-17 15:53:51 -040068 TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesaventoae430302023-05-11 01:42:46 -040069 BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::time_point::max());
Davide Pesavento1816d4b2017-07-02 12:20:48 -040070
71 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
Davide Pesaventoae430302023-05-11 01:42:46 -040072 BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::time_point::max());
Davide Pesavento1816d4b2017-07-02 12:20:48 -040073
74 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesaventoae430302023-05-11 01:42:46 -040075 BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::time_point::max());
Davide Pesavento1816d4b2017-07-02 12:20:48 -040076}
77
Eric Newberry65caf202015-12-17 15:08:16 -070078BOOST_AUTO_TEST_CASE(IdleClose)
79{
Davide Pesavento22fba352017-10-17 15:53:51 -040080 TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020081
Eric Newberry65caf202015-12-17 15:08:16 -070082 int nStateChanges = 0;
Davide Pesavento1816d4b2017-07-02 12:20:48 -040083 transport->afterStateChange.connect(
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040084 [this, &nStateChanges] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -070085 switch (nStateChanges) {
86 case 0:
87 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
88 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
89 break;
90 case 1:
91 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
92 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
93 break;
94 default:
95 BOOST_CHECK(false);
96 }
97 nStateChanges++;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040098 this->limitedIo.afterOp();
Eric Newberry65caf202015-12-17 15:08:16 -070099 });
100
Davide Pesavento14e71f02019-03-28 17:35:25 -0400101 BOOST_REQUIRE_EQUAL(limitedIo.run(2, 8_s), LimitedIo::EXCEED_OPS);
Eric Newberry65caf202015-12-17 15:08:16 -0700102 BOOST_CHECK_EQUAL(nStateChanges, 2);
103}
104
Davide Pesavento22fba352017-10-17 15:53:51 -0400105using RemoteCloseFixture = IpTransportFixture<UnicastUdpTransportFixture,
106 AddressFamily::Any, AddressScope::Loopback>;
Davide Pesaventof56cf632024-01-27 22:22:06 -0500107using RemoteClosePersistencies = boost::mp11::mp_list_c<ndn::nfd::FacePersistency,
108 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
109 ndn::nfd::FACE_PERSISTENCY_PERSISTENT>;
Eric Newberry65caf202015-12-17 15:08:16 -0700110
Davide Pesavento22fba352017-10-17 15:53:51 -0400111BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, Persistency, RemoteClosePersistencies, RemoteCloseFixture)
Eric Newberry65caf202015-12-17 15:08:16 -0700112{
Davide Pesavento22fba352017-10-17 15:53:51 -0400113 TRANSPORT_TEST_INIT(Persistency::value);
Eric Newberry65caf202015-12-17 15:08:16 -0700114
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400115 transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700116 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
117 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
118 this->limitedIo.afterOp();
119 });
120
121 remoteSocket.close();
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400122 transport->send(ndn::encoding::makeStringBlock(300, "hello")); // trigger ICMP error
Davide Pesavento14e71f02019-03-28 17:35:25 -0400123 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry65caf202015-12-17 15:08:16 -0700124
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400125 transport->afterStateChange.connectSingleShot([this] (auto oldState, auto newState) {
Eric Newberry65caf202015-12-17 15:08:16 -0700126 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
127 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
128 this->limitedIo.afterOp();
129 });
130
Davide Pesavento14e71f02019-03-28 17:35:25 -0400131 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry65caf202015-12-17 15:08:16 -0700132}
133
Davide Pesavento22fba352017-10-17 15:53:51 -0400134BOOST_FIXTURE_TEST_CASE(RemoteClosePermanent, RemoteCloseFixture)
Eric Newberry65caf202015-12-17 15:08:16 -0700135{
Davide Pesavento22fba352017-10-17 15:53:51 -0400136 TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
Eric Newberry65caf202015-12-17 15:08:16 -0700137
138 remoteSocket.close();
139
140 Block block1 = ndn::encoding::makeStringBlock(300, "hello");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400141 transport->send(block1);
Eric Newberry65caf202015-12-17 15:08:16 -0700142 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 1);
143 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, block1.size());
144
Davide Pesavento14e71f02019-03-28 17:35:25 -0400145 limitedIo.defer(1_s);
Eric Newberry65caf202015-12-17 15:08:16 -0700146 BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
147
148 remoteConnect();
149
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400150 transport->send(block1);
Eric Newberry65caf202015-12-17 15:08:16 -0700151 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
152 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, 2 * block1.size());
153
154 std::vector<uint8_t> readBuf(block1.size());
155 remoteSocket.async_receive(boost::asio::buffer(readBuf),
156 [this] (const boost::system::error_code& error, size_t) {
157 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400158 limitedIo.afterOp();
Eric Newberry65caf202015-12-17 15:08:16 -0700159 });
Davide Pesavento14e71f02019-03-28 17:35:25 -0400160 BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS);
Eric Newberry65caf202015-12-17 15:08:16 -0700161
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400162 BOOST_TEST(readBuf == block1, boost::test_tools::per_element());
Eric Newberry65caf202015-12-17 15:08:16 -0700163
164 Block block2 = ndn::encoding::makeStringBlock(301, "world");
165 ndn::Buffer buf(block2.begin(), block2.end());
166 remoteWrite(buf);
167
168 BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 1);
169 BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, block2.size());
170 BOOST_CHECK_EQUAL(receivedPackets->size(), 1);
171 BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
Junxiao Shi13546112015-10-14 19:33:07 -0700172}
173
Junxiao Shi13546112015-10-14 19:33:07 -0700174BOOST_AUTO_TEST_SUITE_END() // TestUnicastUdpTransport
175BOOST_AUTO_TEST_SUITE_END() // Face
176
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400177} // namespace nfd::tests