blob: dd3de57bf68f37864a6c70dbe42980c98c773723 [file] [log] [blame]
Yukai Tu16aabbc2015-10-06 05:08:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
Davide Pesavento32065652017-01-15 01:52:21 -05003 * Copyright (c) 2014-2017, Regents of the University of California,
Yukai Tu16aabbc2015-10-06 05:08:42 -07004 * 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 Newberry65caf202015-12-17 15:08:16 -070026#include "transport-test-common.hpp"
27
Eric Newberryf68f3782015-12-11 00:31:32 -070028#include "tcp-transport-fixture.hpp"
Yukai Tu16aabbc2015-10-06 05:08:42 -070029
30namespace nfd {
31namespace face {
32namespace tests {
33
Yukai Tu16aabbc2015-10-06 05:08:42 -070034BOOST_AUTO_TEST_SUITE(Face)
Eric Newberry8717e872015-11-23 12:41:50 -070035BOOST_FIXTURE_TEST_SUITE(TestTcpTransport, TcpTransportFixture)
36
37BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv4)
38{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040039 auto address = getTestIp(AddressFamily::V4, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020040 SKIP_IF_IP_UNAVAILABLE(address);
41 initialize(address);
Eric Newberry8717e872015-11-23 12:41:50 -070042
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 Tu16aabbc2015-10-06 05:08:42 -070051}
52
Eric Newberry8717e872015-11-23 12:41:50 -070053BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv6)
Yukai Tu16aabbc2015-10-06 05:08:42 -070054{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040055 auto address = getTestIp(AddressFamily::V6, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020056 SKIP_IF_IP_UNAVAILABLE(address);
57 initialize(address);
Yukai Tu16aabbc2015-10-06 05:08:42 -070058
Eric Newberry8717e872015-11-23 12:41:50 -070059 checkStaticPropertiesInitialized(*transport);
Yukai Tu16aabbc2015-10-06 05:08:42 -070060
Eric Newberry8717e872015-11-23 12:41:50 -070061 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
69BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv4)
70{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040071 auto address = getTestIp(AddressFamily::V4, AddressScope::Global);
Eric Newberry8717e872015-11-23 12:41:50 -070072 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
87BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv6)
88{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040089 auto address = getTestIp(AddressFamily::V6, AddressScope::Global);
Eric Newberry8717e872015-11-23 12:41:50 -070090 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 Pesavento32065652017-01-15 01:52:21 -0500105BOOST_AUTO_TEST_CASE(PersistencyChange)
106{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400107 auto address = getTestIp(AddressFamily::V4);
Davide Pesavento32065652017-01-15 01:52:21 -0500108 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 Liudcdf6212016-08-31 14:34:22 -0700116BOOST_AUTO_TEST_CASE(PermanentReconnect)
117{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400118 auto address = getTestIp(AddressFamily::V4);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700119 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
138BOOST_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
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400143 auto address = getTestIp(AddressFamily::V4);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700144 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 Pesaventoac238f22017-09-12 15:19:40 -0400157 [&didStateChange] (TransportState oldState, TransportState newState) {
Weiwei Liudcdf6212016-08-31 14:34:22 -0700158 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
166class PermanentTcpTransportReconnectObserver : public TcpTransport
167{
168public:
169 PermanentTcpTransportReconnectObserver(protocol::socket&& socket, LimitedIo& io)
170 : TcpTransport(std::move(socket), ndn::nfd::FACE_PERSISTENCY_PERMANENT)
171 , m_io(io)
172 {
173 }
174
175protected:
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
197private:
198 LimitedIo& m_io;
199};
200
201static double
202asFloatMilliseconds(const time::nanoseconds& t)
203{
204 return static_cast<double>(t.count()) / 1000000.0;
205}
206
207BOOST_AUTO_TEST_CASE(PermanentReconnectWithExponentialBackoff)
208{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400209 auto address = getTestIp(AddressFamily::V4);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700210 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 Tu16aabbc2015-10-06 05:08:42 -0700276BOOST_AUTO_TEST_SUITE_END() // TestTcpTransport
277BOOST_AUTO_TEST_SUITE_END() // Face
278
279} // namespace tests
280} // namespace face
281} // namespace nfd