blob: c73e5e64fbc86695996d2bebeca1fff4d65fc519 [file] [log] [blame]
Junxiao Shi13546112015-10-14 19:33:07 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento32065652017-01-15 01:52:21 -05003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shi13546112015-10-14 19:33:07 -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 Newberry8717e872015-11-23 12:41:50 -070026#include "transport-test-common.hpp"
Junxiao Shi13546112015-10-14 19:33:07 -070027
Eric Newberry65caf202015-12-17 15:08:16 -070028#include "unicast-udp-transport-fixture.hpp"
29
Junxiao Shi13546112015-10-14 19:33:07 -070030namespace nfd {
31namespace face {
32namespace tests {
33
Junxiao Shi13546112015-10-14 19:33:07 -070034BOOST_AUTO_TEST_SUITE(Face)
Eric Newberry65caf202015-12-17 15:08:16 -070035BOOST_FIXTURE_TEST_SUITE(TestUnicastUdpTransport, UnicastUdpTransportFixture)
Junxiao Shi13546112015-10-14 19:33:07 -070036
Eric Newberry65caf202015-12-17 15:08:16 -070037BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv4)
Junxiao Shi13546112015-10-14 19:33:07 -070038{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020039 auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes);
40 SKIP_IF_IP_UNAVAILABLE(address);
41 initialize(address);
Junxiao Shi13546112015-10-14 19:33:07 -070042
Eric Newberry65caf202015-12-17 15:08:16 -070043 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 Shi13546112015-10-14 19:33:07 -070051}
52
Eric Newberry65caf202015-12-17 15:08:16 -070053BOOST_AUTO_TEST_CASE(StaticPropertiesLocalIpv6)
Junxiao Shi13546112015-10-14 19:33:07 -070054{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020055 auto address = getTestIp<ip::address_v6>(LoopbackAddress::Yes);
56 SKIP_IF_IP_UNAVAILABLE(address);
57 initialize(address);
Junxiao Shi13546112015-10-14 19:33:07 -070058
Eric Newberry65caf202015-12-17 15:08:16 -070059 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
69BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv4)
70{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020071 auto address = getTestIp<ip::address_v4>(LoopbackAddress::No);
Eric Newberry65caf202015-12-17 15:08:16 -070072 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
87BOOST_AUTO_TEST_CASE(StaticPropertiesNonLocalIpv6)
88{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020089 auto address = getTestIp<ip::address_v6>(LoopbackAddress::No);
Eric Newberry65caf202015-12-17 15:08:16 -070090 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
Davide Pesavento32065652017-01-15 01:52:21 -0500105BOOST_AUTO_TEST_CASE(PersistencyChange)
106{
107 auto address = getTestIp<ip::address_v4>();
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
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400116BOOST_AUTO_TEST_CASE(ExpirationTime)
117{
118 auto address = getTestIp<ip::address_v4>();
119 SKIP_IF_IP_UNAVAILABLE(address);
120 initialize(address, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
121 BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
122
123 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
124 BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
125
126 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
127 BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
128}
129
Eric Newberry65caf202015-12-17 15:08:16 -0700130BOOST_AUTO_TEST_CASE(IdleClose)
131{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200132 auto address = getTestIp<ip::address_v4>();
133 SKIP_IF_IP_UNAVAILABLE(address);
134 initialize(address, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
135
Eric Newberry65caf202015-12-17 15:08:16 -0700136 int nStateChanges = 0;
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400137 transport->afterStateChange.connect(
Eric Newberry65caf202015-12-17 15:08:16 -0700138 [this, &nStateChanges] (TransportState oldState, TransportState newState) {
139 switch (nStateChanges) {
140 case 0:
141 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
142 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
143 break;
144 case 1:
145 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
146 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
147 break;
148 default:
149 BOOST_CHECK(false);
150 }
151 nStateChanges++;
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400152 limitedIo.afterOp();
Eric Newberry65caf202015-12-17 15:08:16 -0700153 });
154
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400155 BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(8)), LimitedIo::EXCEED_OPS);
Eric Newberry65caf202015-12-17 15:08:16 -0700156 BOOST_CHECK_EQUAL(nStateChanges, 2);
157}
158
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400159using OnDemand = std::integral_constant<ndn::nfd::FacePersistency, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND>;
160using Persistent = std::integral_constant<ndn::nfd::FacePersistency, ndn::nfd::FACE_PERSISTENCY_PERSISTENT>;
161using RemoteClosePersistencies = boost::mpl::vector<OnDemand, Persistent>;
Eric Newberry65caf202015-12-17 15:08:16 -0700162
163BOOST_AUTO_TEST_CASE_TEMPLATE(RemoteClose, Persistency, RemoteClosePersistencies)
164{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200165 auto address = getTestIp<ip::address_v4>();
166 SKIP_IF_IP_UNAVAILABLE(address);
167 initialize(address, Persistency::value);
Eric Newberry65caf202015-12-17 15:08:16 -0700168
169 transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
170 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
171 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
172 this->limitedIo.afterOp();
173 });
174
175 remoteSocket.close();
176 Transport::Packet pkt(ndn::encoding::makeStringBlock(300, "hello"));
177 transport->send(std::move(pkt)); // trigger ICMP error
178 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
179
180 transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
181 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
182 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
183 this->limitedIo.afterOp();
184 });
185
186 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
187}
188
189BOOST_AUTO_TEST_CASE(RemoteClosePermanent)
190{
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200191 auto address = getTestIp<ip::address_v4>();
192 SKIP_IF_IP_UNAVAILABLE(address);
193 initialize(address, ndn::nfd::FACE_PERSISTENCY_PERMANENT);
Eric Newberry65caf202015-12-17 15:08:16 -0700194
195 remoteSocket.close();
196
197 Block block1 = ndn::encoding::makeStringBlock(300, "hello");
198 transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
199 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 1);
200 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, block1.size());
201
202 limitedIo.defer(time::seconds(1));
203 BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
204
205 remoteConnect();
206
207 transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
208 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
209 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, 2 * block1.size());
210
211 std::vector<uint8_t> readBuf(block1.size());
212 remoteSocket.async_receive(boost::asio::buffer(readBuf),
213 [this] (const boost::system::error_code& error, size_t) {
214 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400215 limitedIo.afterOp();
Eric Newberry65caf202015-12-17 15:08:16 -0700216 });
217 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
218
219 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.end(), block1.begin(), block1.end());
220
221 Block block2 = ndn::encoding::makeStringBlock(301, "world");
222 ndn::Buffer buf(block2.begin(), block2.end());
223 remoteWrite(buf);
224
225 BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 1);
226 BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, block2.size());
227 BOOST_CHECK_EQUAL(receivedPackets->size(), 1);
228 BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
Junxiao Shi13546112015-10-14 19:33:07 -0700229}
230
Junxiao Shi13546112015-10-14 19:33:07 -0700231BOOST_AUTO_TEST_SUITE_END() // TestUnicastUdpTransport
232BOOST_AUTO_TEST_SUITE_END() // Face
233
234} // namespace tests
235} // namespace face
236} // namespace nfd