Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -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 | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -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 "transport-test-common.hpp" |
| 27 | |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 28 | #include "tcp-transport-fixture.hpp" |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | namespace face { |
| 32 | namespace tests { |
| 33 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Face) |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestTcpTransport, TcpTransportFixture) |
| 36 | |
| 37 | BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv4) |
| 38 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 39 | auto address = getTestIp<AddressFamily::V4>(LoopbackAddress::Yes); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 40 | SKIP_IF_IP_UNAVAILABLE(address); |
| 41 | initialize(address); |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 42 | |
| 43 | checkStaticPropertiesInitialized(*transport); |
| 44 | |
| 45 | BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("tcp4://127.0.0.1:" + to_string(localEp.port()))); |
| 46 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("tcp4://127.0.0.1:7070")); |
| 47 | BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_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(), MTU_UNLIMITED); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv6) |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 54 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 55 | auto address = getTestIp<AddressFamily::V6>(LoopbackAddress::Yes); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 56 | SKIP_IF_IP_UNAVAILABLE(address); |
| 57 | initialize(address); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 58 | |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 59 | checkStaticPropertiesInitialized(*transport); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 60 | |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("tcp6://[::1]:" + to_string(localEp.port()))); |
| 62 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("tcp6://[::1]:7070")); |
| 63 | BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_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(), MTU_UNLIMITED); |
| 67 | } |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv4) |
| 70 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 71 | auto address = getTestIp<AddressFamily::V4>(LoopbackAddress::No); |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -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("tcp4://" + address.to_string() + ":" + to_string(localEp.port()))); |
| 79 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), |
| 80 | FaceUri("tcp4://" + 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(), MTU_UNLIMITED); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv6) |
| 88 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 89 | auto address = getTestIp<AddressFamily::V6>(LoopbackAddress::No); |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -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("tcp6://[" + address.to_string() + "]:" + to_string(localEp.port()))); |
| 97 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), |
| 98 | FaceUri("tcp6://[" + 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(), MTU_UNLIMITED); |
| 103 | } |
| 104 | |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(PersistencyChange) |
| 106 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 107 | auto address = getTestIp<AddressFamily::V4>(); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 108 | SKIP_IF_IP_UNAVAILABLE(address); |
| 109 | initialize(address); |
| 110 | |
| 111 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND), true); |
| 112 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERSISTENT), true); |
| 113 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true); |
| 114 | } |
| 115 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 116 | BOOST_AUTO_TEST_CASE(PermanentReconnect) |
| 117 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 118 | auto address = getTestIp<AddressFamily::V4>(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 119 | SKIP_IF_IP_UNAVAILABLE(address); |
| 120 | initialize(address, ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 121 | |
| 122 | transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 123 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 124 | BOOST_CHECK_EQUAL(newState, TransportState::DOWN); |
| 125 | limitedIo.afterOp(); |
| 126 | }); |
| 127 | remoteSocket.close(); |
| 128 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 129 | |
| 130 | transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 131 | BOOST_CHECK_EQUAL(oldState, TransportState::DOWN); |
| 132 | BOOST_CHECK_EQUAL(newState, TransportState::UP); |
| 133 | limitedIo.afterOp(); |
| 134 | }); |
| 135 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(ChangePersistencyFromPermanentWhenDown) |
| 139 | { |
| 140 | // when persistency is changed out of permanent while transport is DOWN, |
| 141 | // the transport immediately goes into FAILED state |
| 142 | |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 143 | auto address = getTestIp<AddressFamily::V4>(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 144 | SKIP_IF_IP_UNAVAILABLE(address); |
| 145 | initialize(address, ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 146 | |
| 147 | transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 148 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 149 | BOOST_CHECK_EQUAL(newState, TransportState::DOWN); |
| 150 | limitedIo.afterOp(); |
| 151 | }); |
| 152 | remoteSocket.close(); |
| 153 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 154 | |
| 155 | bool didStateChange = false; |
| 156 | transport->afterStateChange.connectSingleShot( |
Davide Pesavento | ac238f2 | 2017-09-12 15:19:40 -0400 | [diff] [blame] | 157 | [&didStateChange] (TransportState oldState, TransportState newState) { |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 158 | didStateChange = true; |
| 159 | BOOST_CHECK_EQUAL(oldState, TransportState::DOWN); |
| 160 | BOOST_CHECK_EQUAL(newState, TransportState::FAILED); |
| 161 | }); |
| 162 | transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 163 | BOOST_CHECK(didStateChange); |
| 164 | } |
| 165 | |
| 166 | class PermanentTcpTransportReconnectObserver : public TcpTransport |
| 167 | { |
| 168 | public: |
| 169 | PermanentTcpTransportReconnectObserver(protocol::socket&& socket, LimitedIo& io) |
| 170 | : TcpTransport(std::move(socket), ndn::nfd::FACE_PERSISTENCY_PERMANENT) |
| 171 | , m_io(io) |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | protected: |
| 176 | void |
| 177 | reconnect() final |
| 178 | { |
| 179 | TcpTransport::reconnect(); |
| 180 | m_io.afterOp(); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | handleReconnect(const boost::system::error_code& error) final |
| 185 | { |
| 186 | TcpTransport::handleReconnect(error); |
| 187 | m_io.afterOp(); |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | handleReconnectTimeout() final |
| 192 | { |
| 193 | TcpTransport::handleReconnectTimeout(); |
| 194 | m_io.afterOp(); |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | LimitedIo& m_io; |
| 199 | }; |
| 200 | |
| 201 | static double |
| 202 | asFloatMilliseconds(const time::nanoseconds& t) |
| 203 | { |
| 204 | return static_cast<double>(t.count()) / 1000000.0; |
| 205 | } |
| 206 | |
| 207 | BOOST_AUTO_TEST_CASE(PermanentReconnectWithExponentialBackoff) |
| 208 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 209 | auto address = getTestIp<AddressFamily::V4>(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 210 | SKIP_IF_IP_UNAVAILABLE(address); |
| 211 | |
| 212 | tcp::endpoint remoteEp(address, 7070); |
| 213 | startAccept(remoteEp); |
| 214 | |
| 215 | tcp::socket sock(g_io); |
| 216 | sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) { |
| 217 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 218 | limitedIo.afterOp(); |
| 219 | }); |
| 220 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 221 | |
| 222 | auto transportObserver = make_unique<PermanentTcpTransportReconnectObserver>(std::move(sock), |
| 223 | std::ref(limitedIo)); |
| 224 | BOOST_REQUIRE_EQUAL(transportObserver->getState(), TransportState::UP); |
| 225 | |
| 226 | // break the TCP connection |
| 227 | stopAccept(); |
| 228 | remoteSocket.close(); |
| 229 | |
| 230 | // measure retry intervals |
| 231 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 232 | auto retryTime1 = time::steady_clock::now(); |
| 233 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 234 | |
| 235 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 236 | auto retryTime2 = time::steady_clock::now(); |
| 237 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 238 | |
| 239 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 240 | auto retryTime3 = time::steady_clock::now(); |
| 241 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 242 | |
| 243 | // check that the backoff algorithm works |
| 244 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime2 - retryTime1), |
| 245 | asFloatMilliseconds(TcpTransport::s_initialReconnectWait), |
| 246 | 10.0); |
| 247 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime3 - retryTime2), |
| 248 | asFloatMilliseconds(TcpTransport::s_initialReconnectWait) * TcpTransport::s_reconnectWaitMultiplier, |
| 249 | 10.0); |
| 250 | |
| 251 | // reestablish the TCP connection |
| 252 | startAccept(remoteEp); |
| 253 | |
| 254 | BOOST_REQUIRE_EQUAL(limitedIo.run(3, time::seconds(10)), LimitedIo::EXCEED_OPS); |
| 255 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::UP); |
| 256 | |
| 257 | // break the TCP connection again |
| 258 | stopAccept(); |
| 259 | remoteSocket.close(); |
| 260 | |
| 261 | // measure retry intervals |
| 262 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 263 | auto retryTime4 = time::steady_clock::now(); |
| 264 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 265 | |
| 266 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 267 | auto retryTime5 = time::steady_clock::now(); |
| 268 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 269 | |
| 270 | // check that the timeout restarts from the initial value after a successful reconnection |
| 271 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime5 - retryTime4), |
| 272 | asFloatMilliseconds(TcpTransport::s_initialReconnectWait), |
| 273 | 10.0); |
| 274 | } |
| 275 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 276 | BOOST_AUTO_TEST_SUITE_END() // TestTcpTransport |
| 277 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 278 | |
| 279 | } // namespace tests |
| 280 | } // namespace face |
| 281 | } // namespace nfd |