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