Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -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, |
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 | { |
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); |
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 | { |
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); |
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 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 71 | auto address = getTestIp<ip::address_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 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 89 | auto address = getTestIp<ip::address_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 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(PermanentReconnect) |
| 106 | { |
| 107 | auto address = getTestIp<ip::address_v4>(); |
| 108 | SKIP_IF_IP_UNAVAILABLE(address); |
| 109 | initialize(address, ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 110 | |
| 111 | transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 112 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 113 | BOOST_CHECK_EQUAL(newState, TransportState::DOWN); |
| 114 | limitedIo.afterOp(); |
| 115 | }); |
| 116 | remoteSocket.close(); |
| 117 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 118 | |
| 119 | transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 120 | BOOST_CHECK_EQUAL(oldState, TransportState::DOWN); |
| 121 | BOOST_CHECK_EQUAL(newState, TransportState::UP); |
| 122 | limitedIo.afterOp(); |
| 123 | }); |
| 124 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(ChangePersistencyFromPermanentWhenDown) |
| 128 | { |
| 129 | // when persistency is changed out of permanent while transport is DOWN, |
| 130 | // the transport immediately goes into FAILED state |
| 131 | |
| 132 | auto address = getTestIp<ip::address_v4>(); |
| 133 | SKIP_IF_IP_UNAVAILABLE(address); |
| 134 | initialize(address, ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 135 | |
| 136 | transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) { |
| 137 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 138 | BOOST_CHECK_EQUAL(newState, TransportState::DOWN); |
| 139 | limitedIo.afterOp(); |
| 140 | }); |
| 141 | remoteSocket.close(); |
| 142 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 143 | |
| 144 | bool didStateChange = false; |
| 145 | transport->afterStateChange.connectSingleShot( |
| 146 | [this, &didStateChange] (TransportState oldState, TransportState newState) { |
| 147 | didStateChange = true; |
| 148 | BOOST_CHECK_EQUAL(oldState, TransportState::DOWN); |
| 149 | BOOST_CHECK_EQUAL(newState, TransportState::FAILED); |
| 150 | }); |
| 151 | transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 152 | BOOST_CHECK(didStateChange); |
| 153 | } |
| 154 | |
| 155 | class PermanentTcpTransportReconnectObserver : public TcpTransport |
| 156 | { |
| 157 | public: |
| 158 | PermanentTcpTransportReconnectObserver(protocol::socket&& socket, LimitedIo& io) |
| 159 | : TcpTransport(std::move(socket), ndn::nfd::FACE_PERSISTENCY_PERMANENT) |
| 160 | , m_io(io) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | protected: |
| 165 | void |
| 166 | reconnect() final |
| 167 | { |
| 168 | TcpTransport::reconnect(); |
| 169 | m_io.afterOp(); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | handleReconnect(const boost::system::error_code& error) final |
| 174 | { |
| 175 | TcpTransport::handleReconnect(error); |
| 176 | m_io.afterOp(); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | handleReconnectTimeout() final |
| 181 | { |
| 182 | TcpTransport::handleReconnectTimeout(); |
| 183 | m_io.afterOp(); |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | LimitedIo& m_io; |
| 188 | }; |
| 189 | |
| 190 | static double |
| 191 | asFloatMilliseconds(const time::nanoseconds& t) |
| 192 | { |
| 193 | return static_cast<double>(t.count()) / 1000000.0; |
| 194 | } |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE(PermanentReconnectWithExponentialBackoff) |
| 197 | { |
| 198 | auto address = getTestIp<ip::address_v4>(); |
| 199 | SKIP_IF_IP_UNAVAILABLE(address); |
| 200 | |
| 201 | tcp::endpoint remoteEp(address, 7070); |
| 202 | startAccept(remoteEp); |
| 203 | |
| 204 | tcp::socket sock(g_io); |
| 205 | sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) { |
| 206 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
| 207 | limitedIo.afterOp(); |
| 208 | }); |
| 209 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 210 | |
| 211 | auto transportObserver = make_unique<PermanentTcpTransportReconnectObserver>(std::move(sock), |
| 212 | std::ref(limitedIo)); |
| 213 | BOOST_REQUIRE_EQUAL(transportObserver->getState(), TransportState::UP); |
| 214 | |
| 215 | // break the TCP connection |
| 216 | stopAccept(); |
| 217 | remoteSocket.close(); |
| 218 | |
| 219 | // measure retry intervals |
| 220 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 221 | auto retryTime1 = time::steady_clock::now(); |
| 222 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 223 | |
| 224 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 225 | auto retryTime2 = time::steady_clock::now(); |
| 226 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 227 | |
| 228 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 229 | auto retryTime3 = time::steady_clock::now(); |
| 230 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 231 | |
| 232 | // check that the backoff algorithm works |
| 233 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime2 - retryTime1), |
| 234 | asFloatMilliseconds(TcpTransport::s_initialReconnectWait), |
| 235 | 10.0); |
| 236 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime3 - retryTime2), |
| 237 | asFloatMilliseconds(TcpTransport::s_initialReconnectWait) * TcpTransport::s_reconnectWaitMultiplier, |
| 238 | 10.0); |
| 239 | |
| 240 | // reestablish the TCP connection |
| 241 | startAccept(remoteEp); |
| 242 | |
| 243 | BOOST_REQUIRE_EQUAL(limitedIo.run(3, time::seconds(10)), LimitedIo::EXCEED_OPS); |
| 244 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::UP); |
| 245 | |
| 246 | // break the TCP connection again |
| 247 | stopAccept(); |
| 248 | remoteSocket.close(); |
| 249 | |
| 250 | // measure retry intervals |
| 251 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 252 | auto retryTime4 = time::steady_clock::now(); |
| 253 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 254 | |
| 255 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(5)), LimitedIo::EXCEED_OPS); |
| 256 | auto retryTime5 = time::steady_clock::now(); |
| 257 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 258 | |
| 259 | // check that the timeout restarts from the initial value after a successful reconnection |
| 260 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime5 - retryTime4), |
| 261 | asFloatMilliseconds(TcpTransport::s_initialReconnectWait), |
| 262 | 10.0); |
| 263 | } |
| 264 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 265 | BOOST_AUTO_TEST_SUITE_END() // TestTcpTransport |
| 266 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 267 | |
| 268 | } // namespace tests |
| 269 | } // namespace face |
| 270 | } // namespace nfd |