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