blob: 075194b41344106238f0b1f457b27ada62874f62 [file] [log] [blame]
Junxiao Shi13546112015-10-14 19:33:07 -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,
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
Davide Pesavento22fba352017-10-17 15:53:51 -040030#include <boost/mpl/vector.hpp>
31#include <boost/mpl/vector_c.hpp>
32
Junxiao Shi13546112015-10-14 19:33:07 -070033namespace nfd {
34namespace face {
35namespace tests {
36
Junxiao Shi13546112015-10-14 19:33:07 -070037BOOST_AUTO_TEST_SUITE(Face)
Davide Pesavento22fba352017-10-17 15:53:51 -040038BOOST_FIXTURE_TEST_SUITE(TestUnicastUdpTransport, IpTransportFixture<UnicastUdpTransportFixture>)
Junxiao Shi13546112015-10-14 19:33:07 -070039
Davide Pesavento22fba352017-10-17 15:53:51 -040040using UnicastUdpTransportFixtures = boost::mpl::vector<
41 GENERATE_IP_TRANSPORT_FIXTURE_INSTANTIATIONS(UnicastUdpTransportFixture)
42>;
43
44BOOST_FIXTURE_TEST_CASE_TEMPLATE(StaticProperties, T, UnicastUdpTransportFixtures, T)
Junxiao Shi13546112015-10-14 19:33:07 -070045{
Davide Pesavento22fba352017-10-17 15:53:51 -040046 TRANSPORT_TEST_INIT();
Junxiao Shi13546112015-10-14 19:33:07 -070047
Davide Pesavento22fba352017-10-17 15:53:51 -040048 checkStaticPropertiesInitialized(*this->transport);
Eric Newberry65caf202015-12-17 15:08:16 -070049
Davide Pesavento22fba352017-10-17 15:53:51 -040050 BOOST_CHECK_EQUAL(this->transport->getLocalUri(), FaceUri(udp::endpoint(this->address, this->localEp.port())));
51 BOOST_CHECK_EQUAL(this->transport->getRemoteUri(), FaceUri(udp::endpoint(this->address, 7070)));
52 BOOST_CHECK_EQUAL(this->transport->getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); // UDP is never local
53 BOOST_CHECK_EQUAL(this->transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
54 BOOST_CHECK_EQUAL(this->transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
55 BOOST_CHECK_EQUAL(this->transport->getMtu(),
56 this->addressFamily == AddressFamily::V4 ? (65535 - 60 - 8) : (65535 - 8));
Eric Newberry65caf202015-12-17 15:08:16 -070057}
58
Davide Pesavento32065652017-01-15 01:52:21 -050059BOOST_AUTO_TEST_CASE(PersistencyChange)
60{
Davide Pesavento22fba352017-10-17 15:53:51 -040061 TRANSPORT_TEST_INIT();
Davide Pesavento32065652017-01-15 01:52:21 -050062
63 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND), true);
64 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERSISTENT), true);
65 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true);
66}
67
Davide Pesavento1816d4b2017-07-02 12:20:48 -040068BOOST_AUTO_TEST_CASE(ExpirationTime)
69{
Davide Pesavento22fba352017-10-17 15:53:51 -040070 TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesavento1816d4b2017-07-02 12:20:48 -040071 BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
72
73 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
74 BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
75
76 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
77 BOOST_CHECK_NE(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
78}
79
Eric Newberry65caf202015-12-17 15:08:16 -070080BOOST_AUTO_TEST_CASE(IdleClose)
81{
Davide Pesavento22fba352017-10-17 15:53:51 -040082 TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020083
Eric Newberry65caf202015-12-17 15:08:16 -070084 int nStateChanges = 0;
Davide Pesavento1816d4b2017-07-02 12:20:48 -040085 transport->afterStateChange.connect(
Eric Newberry65caf202015-12-17 15:08:16 -070086 [this, &nStateChanges] (TransportState oldState, TransportState newState) {
87 switch (nStateChanges) {
88 case 0:
89 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
90 BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
91 break;
92 case 1:
93 BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
94 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
95 break;
96 default:
97 BOOST_CHECK(false);
98 }
99 nStateChanges++;
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400100 limitedIo.afterOp();
Eric Newberry65caf202015-12-17 15:08:16 -0700101 });
102
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400103 BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(8)), LimitedIo::EXCEED_OPS);
Eric Newberry65caf202015-12-17 15:08:16 -0700104 BOOST_CHECK_EQUAL(nStateChanges, 2);
105}
106
Davide Pesavento22fba352017-10-17 15:53:51 -0400107using RemoteCloseFixture = IpTransportFixture<UnicastUdpTransportFixture,
108 AddressFamily::Any, AddressScope::Loopback>;
109using RemoteClosePersistencies = boost::mpl::vector_c<ndn::nfd::FacePersistency,
110 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
111 ndn::nfd::FACE_PERSISTENCY_PERSISTENT>;
Eric Newberry65caf202015-12-17 15:08:16 -0700112
Davide Pesavento22fba352017-10-17 15:53:51 -0400113BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, Persistency, RemoteClosePersistencies, RemoteCloseFixture)
Eric Newberry65caf202015-12-17 15:08:16 -0700114{
Davide Pesavento22fba352017-10-17 15:53:51 -0400115 TRANSPORT_TEST_INIT(Persistency::value);
Eric Newberry65caf202015-12-17 15:08:16 -0700116
117 transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
118 BOOST_CHECK_EQUAL(oldState, TransportState::UP);
119 BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
120 this->limitedIo.afterOp();
121 });
122
123 remoteSocket.close();
124 Transport::Packet pkt(ndn::encoding::makeStringBlock(300, "hello"));
125 transport->send(std::move(pkt)); // trigger ICMP error
126 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
127
128 transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
129 BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
130 BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
131 this->limitedIo.afterOp();
132 });
133
134 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
135}
136
Davide Pesavento22fba352017-10-17 15:53:51 -0400137BOOST_FIXTURE_TEST_CASE(RemoteClosePermanent, RemoteCloseFixture)
Eric Newberry65caf202015-12-17 15:08:16 -0700138{
Davide Pesavento22fba352017-10-17 15:53:51 -0400139 TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
Eric Newberry65caf202015-12-17 15:08:16 -0700140
141 remoteSocket.close();
142
143 Block block1 = ndn::encoding::makeStringBlock(300, "hello");
144 transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
145 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 1);
146 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, block1.size());
147
148 limitedIo.defer(time::seconds(1));
149 BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
150
151 remoteConnect();
152
153 transport->send(Transport::Packet{Block{block1}}); // make a copy of the block
154 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
155 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, 2 * block1.size());
156
157 std::vector<uint8_t> readBuf(block1.size());
158 remoteSocket.async_receive(boost::asio::buffer(readBuf),
159 [this] (const boost::system::error_code& error, size_t) {
160 BOOST_REQUIRE_EQUAL(error, boost::system::errc::success);
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400161 limitedIo.afterOp();
Eric Newberry65caf202015-12-17 15:08:16 -0700162 });
163 BOOST_REQUIRE_EQUAL(limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
164
165 BOOST_CHECK_EQUAL_COLLECTIONS(readBuf.begin(), readBuf.end(), block1.begin(), block1.end());
166
167 Block block2 = ndn::encoding::makeStringBlock(301, "world");
168 ndn::Buffer buf(block2.begin(), block2.end());
169 remoteWrite(buf);
170
171 BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 1);
172 BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, block2.size());
173 BOOST_CHECK_EQUAL(receivedPackets->size(), 1);
174 BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
Junxiao Shi13546112015-10-14 19:33:07 -0700175}
176
Junxiao Shi13546112015-10-14 19:33:07 -0700177BOOST_AUTO_TEST_SUITE_END() // TestUnicastUdpTransport
178BOOST_AUTO_TEST_SUITE_END() // Face
179
180} // namespace tests
181} // namespace face
182} // namespace nfd