Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 30 | namespace nfd { |
| 31 | namespace face { |
| 32 | namespace tests { |
| 33 | |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Face) |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestUnicastUdpTransport, UnicastUdpTransportFixture) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 36 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv4) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 38 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 39 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes); |
| 40 | SKIP_IF_IP_UNAVAILABLE(address); |
| 41 | initialize(address); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 42 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 43 | checkStaticPropertiesInitialized(*transport); |
| 44 | |
| 45 | BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("udp4://127.0.0.1:" + to_string(localEp.port()))); |
| 46 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("udp4://127.0.0.1:7070")); |
| 47 | BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local |
| 48 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 49 | BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 50 | BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 60 - 8); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv6) |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 54 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 55 | auto address = getTestIp<ip::address_v6>(LoopbackAddress::Yes); |
| 56 | SKIP_IF_IP_UNAVAILABLE(address); |
| 57 | initialize(address); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 58 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 59 | checkStaticPropertiesInitialized(*transport); |
| 60 | |
| 61 | BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("udp6://[::1]:" + to_string(localEp.port()))); |
| 62 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("udp6://[::1]:7070")); |
| 63 | BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local |
| 64 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 65 | BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 66 | BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 8); |
| 67 | } |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv4) |
| 70 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 71 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::No); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 72 | SKIP_IF_IP_UNAVAILABLE(address); |
| 73 | initialize(address); |
| 74 | |
| 75 | checkStaticPropertiesInitialized(*transport); |
| 76 | |
| 77 | BOOST_CHECK_EQUAL(transport->getLocalUri(), |
| 78 | FaceUri("udp4://" + address.to_string() + ":" + to_string(localEp.port()))); |
| 79 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), |
| 80 | FaceUri("udp4://" + address.to_string() + ":7070")); |
| 81 | BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 82 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 83 | BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 84 | BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 60 - 8); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv6) |
| 88 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 89 | auto address = getTestIp<ip::address_v6>(LoopbackAddress::No); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 90 | SKIP_IF_IP_UNAVAILABLE(address); |
| 91 | initialize(address); |
| 92 | |
| 93 | checkStaticPropertiesInitialized(*transport); |
| 94 | |
| 95 | BOOST_CHECK_EQUAL(transport->getLocalUri(), |
| 96 | FaceUri("udp6://[" + address.to_string() + "]:" + to_string(localEp.port()))); |
| 97 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), |
| 98 | FaceUri("udp6://[" + address.to_string() + "]:7070")); |
| 99 | BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 100 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 101 | BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 102 | BOOST_CHECK_EQUAL(transport->getMtu(), 65535 - 8); |
| 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(IdleClose) |
| 106 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 107 | auto address = getTestIp<ip::address_v4>(); |
| 108 | SKIP_IF_IP_UNAVAILABLE(address); |
| 109 | initialize(address, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 110 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 111 | BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::TimePoint::max()); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 112 | |
| 113 | int nStateChanges = 0; |
| 114 | this->transport->afterStateChange.connect( |
| 115 | [this, &nStateChanges] (TransportState oldState, TransportState newState) { |
| 116 | switch (nStateChanges) { |
| 117 | case 0: |
| 118 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 119 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSING); |
| 120 | break; |
| 121 | case 1: |
| 122 | BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING); |
| 123 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSED); |
| 124 | break; |
| 125 | default: |
| 126 | BOOST_CHECK(false); |
| 127 | } |
| 128 | nStateChanges++; |
| 129 | this->limitedIo.afterOp(); |
| 130 | }); |
| 131 | |
| 132 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(10)), LimitedIo::EXCEED_OPS); |
| 133 | |
| 134 | BOOST_CHECK_EQUAL(nStateChanges, 2); |
| 135 | } |
| 136 | |
| 137 | typedef std::integral_constant<ndn::nfd::FacePersistency, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND> OnDemand; |
| 138 | typedef std::integral_constant<ndn::nfd::FacePersistency, ndn::nfd::FACE_PERSISTENCY_PERSISTENT> Persistent; |
| 139 | typedef boost::mpl::vector<OnDemand, Persistent> RemoteClosePersistencies; |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE_TEMPLATE(RemoteClose, Persistency, RemoteClosePersistencies) |
| 142 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 143 | auto address = getTestIp<ip::address_v4>(); |
| 144 | SKIP_IF_IP_UNAVAILABLE(address); |
| 145 | initialize(address, Persistency::value); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 146 | |
| 147 | transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 148 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 149 | BOOST_CHECK_EQUAL(newState, TransportState::FAILED); |
| 150 | this->limitedIo.afterOp(); |
| 151 | }); |
| 152 | |
| 153 | remoteSocket.close(); |
| 154 | Transport::Packet pkt(ndn::encoding::makeStringBlock(300, "hello")); |
| 155 | transport->send(std::move(pkt)); // trigger ICMP error |
| 156 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 157 | |
| 158 | transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 159 | BOOST_CHECK_EQUAL(oldState, TransportState::FAILED); |
| 160 | BOOST_CHECK_EQUAL(newState, TransportState::CLOSED); |
| 161 | this->limitedIo.afterOp(); |
| 162 | }); |
| 163 | |
| 164 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 165 | } |
| 166 | |
| 167 | BOOST_AUTO_TEST_CASE(RemoteClosePermanent) |
| 168 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 169 | auto address = getTestIp<ip::address_v4>(); |
| 170 | SKIP_IF_IP_UNAVAILABLE(address); |
| 171 | initialize(address, ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 172 | |
| 173 | remoteSocket.close(); |
| 174 | |
| 175 | Block block1 = ndn::encoding::makeStringBlock(300, "hello"); |
| 176 | transport->send(Transport::Packet{Block{block1}}); // make a copy of the block |
| 177 | BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 1); |
| 178 | BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, block1.size()); |
| 179 | |
| 180 | limitedIo.defer(time::seconds(1)); |
| 181 | BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP); |
| 182 | |
| 183 | remoteConnect(); |
| 184 | |
| 185 | transport->send(Transport::Packet{Block{block1}}); // make a copy of the block |
| 186 | BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2); |
| 187 | BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, 2 * block1.size()); |
| 188 | |
| 189 | std::vector<uint8_t> readBuf(block1.size()); |
| 190 | remoteSocket.async_receive(boost::asio::buffer(readBuf), |
| 191 | [this] (const boost::system::error_code& error, size_t) { |
| 192 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 193 | this->limitedIo.afterOp(); |
| 194 | }); |
| 195 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 196 | |
| 197 | BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.end(), block1.begin(), block1.end()); |
| 198 | |
| 199 | Block block2 = ndn::encoding::makeStringBlock(301, "world"); |
| 200 | ndn::Buffer buf(block2.begin(), block2.end()); |
| 201 | remoteWrite(buf); |
| 202 | |
| 203 | BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 1); |
| 204 | BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, block2.size()); |
| 205 | BOOST_CHECK_EQUAL(receivedPackets->size(), 1); |
| 206 | BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 209 | BOOST_AUTO_TEST_CASE(ChangePersistencyNoExpirationTime) |
| 210 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 211 | auto address = getTestIp<ip::address_v4>(); |
| 212 | SKIP_IF_IP_UNAVAILABLE(address); |
| 213 | initialize(address, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 214 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 215 | BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::TimePoint::max()); |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 216 | transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 217 | BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max()); |
| 218 | } |
| 219 | |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 220 | BOOST_AUTO_TEST_SUITE_END() // TestUnicastUdpTransport |
| 221 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 222 | |
| 223 | } // namespace tests |
| 224 | } // namespace face |
| 225 | } // namespace nfd |